PostgreSQL中的PuttingmultipleLIKEpatternsintoanarray特性是什么

本篇内容主要讲解“PostgreSQL中的Putting multiple LIKE patterns into an array特性是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL中的Putting multiple LIKE patterns into an array特性是什么”吧!

曲阜网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

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特性是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网页题目:PostgreSQL中的PuttingmultipleLIKEpatternsintoanarray特性是什么
转载源于:http://pwwzsj.com/article/pghhcp.html