我想使用具有多个条件的 LIKE 运算符来选择记录,我可以OR
在条件之间使用,但我正在寻找一种解决此问题的简短方法。
我正在尝试以下查询:
select *
from employees
where name like '%.%'
OR name like '%,%'
OR name like '%;%'
OR name like '%/%' -- this code goes very long
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来缩短这个查询。
提前感谢任何帮助。
小智 13
您可以将LIKE
运算符与数组结合使用:
select *
from employees
where name like any (array['%.%', '%,%', %;%', ...]);
Run Code Online (Sandbox Code Playgroud)
或者一种完全不同的方法,不需要为每个值重复通配符:
select e.*
from employees e
where exists (select *
from (values(('.'), (','), (';'))) as t(ch)
where e.name like concat('%', t.ch, '%'))
Run Code Online (Sandbox Code Playgroud)
由于您只检查单个字符,因此您也可以使用 Postgres 的数组函数:将名称转换为字符数组并检查重叠数组:
select *
from employees
where string_to_array(name, null) && array['.', ',', ';'];
Run Code Online (Sandbox Code Playgroud)
或者
select *
from employees
where string_to_array(name, null) && string_to_array('.,;', null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37121 次 |
最近记录: |