共计 1833 个字符,预计需要花费 5 分钟才能阅读完成。
行业资讯
数据库
关系型数据库
PostgreSQL 中的 Putting multiple LIKE patterns into an array 特性是什么
本篇内容主要讲解“PostgreSQL 中的 Putting multiple LIKE patterns into an array 特性是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“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 特性是什么”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!