本篇内容主要讲解“PostgreSQL中的Putting multiple LIKE patterns into an array特性是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL中的Putting multiple LIKE patterns into an array特性是什么”吧!
LIKE用于模糊查询,如select from tablename where column like ‘%value%’;如需要多个模糊匹配则需要使用多个like子句,如select from tablename where column like ‘%value1%’ or column like ‘%value2%’ or column like ‘%value3%’或者select * from tablename where column like ‘%value1%’ and column like ‘%value2%’ and column like ‘%value3%’。PG提供了ANY/ALL结合ARRAY来简化SQL:
select
from tablename where column like ‘%value1%’ or column like ‘%value2%’ or column like ‘%value3%’
->
select from tablename where column like ANY(ARRAY[‘%value1%’,’%value2%’,’%value3%’])
select
from tablename where column like ‘%value1%’ and column like ‘%value2%’ and column like ‘%value3%’
->
select from tablename where column like ALL(ARRAY[‘%value1%’,’%value2%’,’%value3%’])
测试脚本
[local]:5432 pg12@testdb=# create table t_like (id int,c1 varchar(20));
CREATE TABLE
Time: 160.410 ms
[local]:5432 pg12@testdb=#
[local]:5432 pg12@testdb=# insert into t_like(id,c1) values(1,'asdfafetestsdfasdf');
INSERT 0 1
Time: 14.815 ms
[local]:5432 pg12@testdb=# insert into t_like(id,c1) values(2,'asdfafe测试sdfasdf');
INSERT 0 1
Time: 1.423 ms
[local]:5432 pg12@testdb=# insert into t_like(id,c1) values(3,'astestfe测试sdfasdf');
INSERT 0 1
Time: 1.486 ms
[local]:5432 pg12@testdb=# select * from t_like where c1 like ANY(ARRAY['%test%','%测试%']);
id | c1
----+---------------------
1 | asdfafetestsdfasdf
2 | asdfafe测试sdfasdf
3 | astestfe测试sdfasdf
(3 rows)
Time: 48.319 ms
[local]:5432 pg12@testdb=# select * from t_like where c1 like ALL(ARRAY['%test%','%测试%']);
id | c1
----+---------------------
3 | astestfe测试sdfasdf
(1 row)
Time: 2.463 ms
[local]:5432 pg12@testdb=#
到此,相信大家对“PostgreSQL中的Putting multiple LIKE patterns into an array特性是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!