循环查询直到找到结果

发布于 2025-01-25 06:13:46 字数 1797 浏览 2 评论 0原文

我很难创建一个简单的“循环”。

如何循环选择查询并删除字符串的最后一个字符,直到找到结果?

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2025-02-01 06:13:46

您需要为此一个

倾斜

 创建表选项卡
    (`myfield` varchar(7))
;
    
插入选项卡
    (`Myfield')
值
    ('1stone'),
    ('1stick'),
    ('1stove'),
    ('1stones'),
    ('2stick'),
    ('2stove'),
    ('2stone'),
    ('2sticks'),
    ('3 stoves'),
    ('3stone'),
    ('3stick'),
    ('3 stove')
;
 
 创建过程procedure_name(_searchText varchar(100))
开始
   声明_mylen int默认为0;
   set _mylen:=长度(_searchText);
   myloop:_mylen&gt; 0做
       
       将@sql ='Select Count(*)设置为@i,从tab中的where myfield喜欢?';
        set @text:=(select concat(左(_searchText,_mylen),'%'));
        从@SQL准备STMT;
        使用@Text执行STMT;
        DealLocate准备STMT;
       如果@i&gt; 0然后
           设置@sql ='从tab喜欢的tab喜欢?';';';
        从@SQL准备STMT;
        使用@Text执行STMT;
        DealLocate准备STMT;           
           离开myloop;
       如果结束;
       设置_mylen:= _mylen -1;
   结束时;
   如果@i = 0,则
       结果选择“无条目”;
   如果结束;
结尾;
 
 调用Procedure_Name('1Story')
 
 | Myfield |
| :------- |
| 1stone |
| 1 Stove |
| 1stones |

✓
 调用Procedure_Name('ABC')
 
 |结果|
| :---------- |
|没有条目|

✓

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

CREATE TABLE tab
    (`myfield` varchar(7))
;
    
INSERT INTO tab
    (`myfield`)
VALUES
    ('1stone'),
    ('1stick'),
    ('1stove'),
    ('1stones'),
    ('2stick'),
    ('2stove'),
    ('2stone'),
    ('2sticks'),
    ('3stoves'),
    ('3stone'),
    ('3stick'),
    ('3stove')
;
CREATE PROCEDURE procedure_name(_searchtext varchar(100))
BEGIN
   DECLARE _mylen int DEFAULT 0;
   SET _mylen := LENGTH(_searchtext);
   myloop: WHILE _myLen > 0 DO
       
       SET @sql = 'SELECT COUNT(*) INTO @i FROM tab WHERE myfield LIKE  ?';
        set @text := (SELECT CONCAT(LEFT(_searchtext,_mylen), '%'));
        PREPARE stmt FROM @sql;
        EXECUTE stmt USING @text;
        DEALLOCATE PREPARE stmt;
       IF @i > 0 then
           SET @sql = 'SELECT myfield FROM tab WHERE myfield LIKE  ?';
        PREPARE stmt FROM @sql;
        EXECUTE stmt USING @text;
        DEALLOCATE PREPARE stmt;           
           LEAVE myloop;
       end if;
       SET _myLen := _myLen -1;
   END WHILE;
   IF  @i = 0 then
       SELECT 'No entries' AS result;
   END IF;
END;
CALL procedure_name('1story')
| myfield |
| :------ |
| 1stone  |
| 1stove  |
| 1stones |

✓
CALL procedure_name('abc')
| result     |
| :--------- |
| No entries |

✓

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文