有没有更短的方法来查找多个匹配:
SELECT *
from table
WHERE column LIKE "AAA%"
OR column LIKE "BBB%"
OR column LIKE "CCC%"
这个问题适用于 PostgreSQL 9.1,但如果有一个通用的解决方案那就更好了。
也许使用
SIMILAR TO
会有效?
SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%';
使用数组或集合比较:
create table t (str text);
insert into t values ('AAA'), ('BBB'), ('DDD999YYY'), ('DDD099YYY');
select str from t
where str like any ('{"AAA%", "BBB%", "CCC%"}');
select str from t
where str like any (values('AAA%'), ('BBB%'), ('CCC%'));
也可以做一个
AND
,如果要匹配任何订单,这对于正则表达式来说并不容易:
select str from t
where str like all ('{"%999%", "DDD%"}');
select str from t
where str like all (values('%999%'), ('DDD%'));
您可以使用正则表达式运算符 (~),用 (|) 分隔,如模式匹配
中所述select column_a from table where column_a ~* 'aaa|bbb|ccc'
以下查询对我有帮助。您可以使用
LIKE
,而不是使用 ~*
。
select id, name from hosts where name ~* 'julia|lena|jack';
如果您想不喜欢跳过值,请使用此查询示例:
select * from table
where not field like any(array['%aaa%','%bbb%','%ccc%','%ddd%'])
如果您实际上不需要通配符,您也许可以使用 IN。
选择* 从表 WHERE 列 IN ('AAA', 'BBB', 'CCC')