单个模糊查询一般使用like,如果多个可以使用 OR 进行连接,不过写样子写法很冗余,而且如果多个条件是从表中 select出来的时候这种方法就不可行了。
针对这种问题,一般都提供了正则表达式的写法,这样我上面说的那种情况只需要进行一下列转行就可以了。
注意 REGEXP 后是 字符串类型
MySQL
-- 示例
SELECT prod_name FROM  products WHERE prod_name REGEXP ‘col1|col2’

-- 列转行
select group_concat(testcol) from tableA;

-- 完整版
SELECT prod_name FROM  products WHERE prod_name REGEXP (select 
replace(group_concat(testcol),'\'','|')
 from tableA);

类似的,Oracle也是类似的
-- Oracle
select  * from tablex where REGEXP_LIKE(字段名, '(匹配串1|匹配串2|...)') ;

select  * from tablex where REGEXP_LIKE(字段名, '^(匹配串1|匹配串2|...)') ";

select  * from tablex where REGEXP_LIKE(字段名, '(匹配串1|匹配串2|...)$') ";

 
-- SqlServer
select  * from tablex where f1 like '%[匹配串1|匹配串2|匹配串3]%';
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐