单个模糊查询一般使用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(selectreplace(group_concat(testcol),'\'','|')from tableA);
类似的,Oracle也是类似的
-- Oracleselect*from tablex where REGEXP_LIKE(字段名,'(匹配串1|匹配串2|...)');select*from tablex where REGEXP_LIKE(字段名,'^(匹配串1|匹配串2|...)')";
select * from tablex where REGEXP_LIKE(字段名, '(匹配串1|匹配串2|...)$') ";-- SqlServerselect*from tablex where f1 like'%[匹配串1|匹配串2|匹配串3]%';