select 来自 table where condition like %{list of values}%

select from table where condition like %{list of values}%

我想获取 table 中的所有行,其中“comment”列可以包含另一个 table 的值:

table T1 中的行名称包含名称列表。

行注释table T2复合一个字符串,注释中可以嵌入一个名字

我尝试执行下一个 PLSQL 代码:

BEGIN

SET @SearchCriteria = (SELECT * FROM names);
    
SET @SearchCriteria = '%' + @SearchCriteria + '%';
    
SELECT * FROM ticket WHERE comment LIKE @SearchCriteria;

END

但是它returns: 子查询returns 多于 1 行

此外,我执行了以下程序,但没有得到任何结果:

BEGIN 
DECLARE nm VARCHAR(16);
DECLARE cur1 CURSOR FOR SELECT nm FROM names;

OPEN cur1;
   
   read_loop: LOOP
    FETCH cur1 INTO nm;
    set @name = '%'+nm+'%';
  
    SELECT * FROM ticket WHERE comment LIKE @name;
    
   END LOOP;
   CLOSE cur1; 
END

之前的代码需要很长时间才能得到结果,所以我在phpmyadmin.conf中更改了max_execution_time的值。

MYSQL版本:5.6.17

怎样才能得到正确的结果?

您可以通过单个查询完成此操作:

select t.*
from ticket t
where exists (
    select 1
    from names n
    where t.comment like concat('%', n.nm, '%')
)

这会带来来自 table ticket 的所有行,其 comment 包含可以在 table names 中找到的 nm