循环查询直到找到结果
我很难创建一个简单的“循环”。
如何循环选择查询并删除字符串的最后一个字符,直到找到结果?
例如:
myfield
_______
1stone
1stick
1stove
1stones
2stick
2stove
2stone
2sticks
3stoves
3stone
3stick
3stove
[loop1] SELECT myfield FROM table WHERE myfield LIKE '1story%' -> 0 results
[loop2] SELECT myfield FROM table WHERE myfield LIKE '1stor%' -> 0 results
[loop3] SELECT myfield FROM table WHERE myfield LIKE '1sto%' -> 3 results
需要导致
1stone
1stones
1stove
或
[loop1] SELECT myfield FROM table WHERE myfield LIKE '2somevalue%' -> 0 results
[loop2] SELECT myfield FROM table WHERE myfield LIKE '2somevalu%' -> 0 results
[loop3] SELECT myfield FROM table WHERE myfield LIKE '2someval%' -> 0 results
[loop4] SELECT myfield FROM table WHERE myfield LIKE '2someva%' -> 0 results
[loop5] SELECT myfield FROM table WHERE myfield LIKE '2somev%' -> 0 results
[loop6] SELECT myfield FROM table WHERE myfield LIKE '2some%' -> 0 results
[loop7] SELECT myfield FROM table WHERE myfield LIKE '2som%' -> 0 results
[loop8] SELECT myfield FROM table WHERE myfield LIKE '2so%' -> 0 results
[loop9] SELECT myfield FROM table WHERE myfield LIKE '2s%' -> 4 results
需要导致
2stick
2sticks
2stone
2stove
我一直在尝试,但是到目前为止没有运气
SET @counter = 0, @results = 0, @value = 'somevalue'
WHILE ( @counter <= LENGTH(@value) AND @results = 0)
BEGIN
SET @qry = "SELECT COUNT(myfield) FROM table WHERE myfield LIKE '" + LEFT(@value, LENGTH(@value)-@Counter) + "%' "
SET @results = (SELECT COUNT(myfield) FROM table WHERE myfield LIKE LEFT(@value, LENGTH(@value)-@Counter) + '%')
SET @counter = @Counter + 1
END
exec @qry
i'm having trouble creating a simple 'while loop'.
How can i loop a select query and remove the last character of a string until results are found?
eg.:
myfield
_______
1stone
1stick
1stove
1stones
2stick
2stove
2stone
2sticks
3stoves
3stone
3stick
3stove
[loop1] SELECT myfield FROM table WHERE myfield LIKE '1story%' -> 0 results
[loop2] SELECT myfield FROM table WHERE myfield LIKE '1stor%' -> 0 results
[loop3] SELECT myfield FROM table WHERE myfield LIKE '1sto%' -> 3 results
would need to result in
1stone
1stones
1stove
or
[loop1] SELECT myfield FROM table WHERE myfield LIKE '2somevalue%' -> 0 results
[loop2] SELECT myfield FROM table WHERE myfield LIKE '2somevalu%' -> 0 results
[loop3] SELECT myfield FROM table WHERE myfield LIKE '2someval%' -> 0 results
[loop4] SELECT myfield FROM table WHERE myfield LIKE '2someva%' -> 0 results
[loop5] SELECT myfield FROM table WHERE myfield LIKE '2somev%' -> 0 results
[loop6] SELECT myfield FROM table WHERE myfield LIKE '2some%' -> 0 results
[loop7] SELECT myfield FROM table WHERE myfield LIKE '2som%' -> 0 results
[loop8] SELECT myfield FROM table WHERE myfield LIKE '2so%' -> 0 results
[loop9] SELECT myfield FROM table WHERE myfield LIKE '2s%' -> 4 results
would need to result in
2stick
2sticks
2stone
2stove
i've been trying this but with no luck so far
SET @counter = 0, @results = 0, @value = 'somevalue'
WHILE ( @counter <= LENGTH(@value) AND @results = 0)
BEGIN
SET @qry = "SELECT COUNT(myfield) FROM table WHERE myfield LIKE '" + LEFT(@value, LENGTH(@value)-@Counter) + "%' "
SET @results = (SELECT COUNT(myfield) FROM table WHERE myfield LIKE LEFT(@value, LENGTH(@value)-@Counter) + '%')
SET @counter = @Counter + 1
END
exec @qry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为此一个
倾斜
db&lt;&gt;&gt;
You need a procrdure for this as i said
This uses dynamic sql because the code else would be vulnerable to sql injection
db<>fiddle here