SQL查询结合like和in的使用方法?

42 浏览
0 Comments

SQL查询结合like和in的使用方法?

这个问题已经有答案了:

SQL中有“LIKE”和“IN”的组合吗?

a. Select * from tableA where columnA like '%complete%';
b. Select * from tableA where columnA in ('complete','request');

列A可能的值为complete,completed,request,requested......

我的目标是查询那些值为complete,completed,request,requested的记录

通常我们会写查询where columnA in (\'complete\',\'completed\',\'request\',\'requested\');

有没有办法写一个更简短的查询,像 Select * from tableA where columnA in like (%complete%,%request%)

admin 更改状态以发布 2023年5月24日
0
0 Comments

您可以尝试这样使用CONTAINS: \n\n

select * from tableA
WHERE CONTAINS(columnA, '"complete*" OR "requested*" OR ...')

0
0 Comments

你需要使用多个 OR

select * 
from tableA 
where columnA like '%complete%'
   or columnA like '%requested%'
   or ...

使用 Join:

SELECT *
FROM tableA t
JOIN VALUES (('%complete%'), ('%requested%'), ...) v(c)
  ON t.columnA LIKE v.c

请注意,搜索模式 %phrase% 不是 SARG 可用的,如果存在任何索引,查询优化器将不会使用该列上的索引。

您应该考虑使用全文搜索

0