oracle数据库模糊查询匹配多个字符串

一、查询包含某些字段的记录(包含其中一个就符合):

对需要匹配的字符串建一个表 KEY_TABLE

ID KEYWORD TYPE
1 字符串1 1
2 字符串2 1
3 字符串3 1

SELECT * FROM MAIN_TABLE mt ,KEY_TABLE s
WHERE mt.matching LIKE '%'||s.keyword||'%' (or)

二、 查询不包含这些字段的记录:

1、SELECT * FROM MAIN_TABLE mt 
WHERE  not regexp_like (mt.matching,'字符串1|字符串2|字符串3');

(也可以利用列转行函数:

SELECT * FROM MAIN_TABLE mt ,(SELECT t.type,listagg(t.keyword,'|') WITHIN GROUP(ORDER BY t.type) AS allkey
FROM KEY_TABLE t GROUP BY t.type
) kt

WHERE  not regexp_like (mt.matching,kt.keyword)  and kt.type=1;

2、SELECT * FROM MAIN_TABLE mt 
WHERE mt.matching  not like '%字符串1%' and  mt.matching  not like '%字符串2%'  and mt.matching  not like '%字符串3%'; (效率比较高)

posted @ 2018-11-15 11:36  爱喝毒奶粉  阅读(11487)  评论(0编辑  收藏  举报