sor*_*rin 33 sql postgresql postgresql-9.1
有没有更短的方法来寻找多个匹配:
SELECT *
from table
WHERE column LIKE "AAA%"
OR column LIKE "BBB%"
OR column LIKE "CCC%"
Run Code Online (Sandbox Code Playgroud)
这个问题适用于PostgreSQL 9.1,但如果有一个通用的解决方案,它会更好.
toz*_*zka 41
也许使用SIMILAR TO会起作用吗?
SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%';
Run Code Online (Sandbox Code Playgroud)
Clo*_*eto 20
使用数组或集合比较:
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%'));
Run Code Online (Sandbox Code Playgroud)
AND如果它匹配任何顺序,也可以使用正则表达式做一个不容易的事情:
select str from t
where str like all ('{"%999%", "DDD%"}');
select str from t
where str like all (values('%999%'), ('DDD%'));
Run Code Online (Sandbox Code Playgroud)
小智 9
您可以使用正则表达式运算符 ( ~ ),以 ( | )分隔,如模式匹配中所述
select column_a from table where column_a ~* 'aaa|bbb|ccc'
Run Code Online (Sandbox Code Playgroud)
小智 6
以下查询对我有帮助。LIKE您可以使用来代替使用~*。
select id, name from hosts where name ~* 'julia|lena|jack';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
160799 次 |
| 最近记录: |