当前位置:Gxlcms > 数据库问题 > postgresql----LIKE和SIMILAR TO

postgresql----LIKE和SIMILAR TO

时间:2021-07-01 10:21:17 帮助过:5人阅读

 

示例表:

test=# drop table if exists tbl_insert;
DROP TABLE
test=# create table tbl_insert(a int,b int,c varchar(12));
CREATE TABLE
test=# insert into tbl_insert(a,b,c) values (1,1,11),(2,2,22),(3,3,33),(4,4,44),(5,5,51),(6,6,1),(6,6,61),(6,6,661),(7,7,3%1),
(8,8,3%_1),(8,8,3_%_1),(7,7,abc),(7,7,ABc),(7,7,aBC); INSERT 0 14

一.LIKE和ILIKE

1.查询字段c是以‘1‘结束,且‘1‘字符前有且只有一个字符的行。

 

test=# select * from tbl_insert where c like _1;
 a | b | c  
---+---+----
 1 | 1 | 11
 5 | 5 | 51
 6 | 6 | 61
(3 rows)

2.查询字段c以‘1‘结束的行

 

test=# select * from tbl_insert where c like %1;
 a | b |   c   
---+---+-------
 1 | 1 | 11
 5 | 5 | 51
 6 | 6 | 1
 6 | 6 | 61
 6 | 6 | 661
 7 | 7 | 3%1
 8 | 8 | 3%_1
 8 | 8 | 3_%_1
(8 rows)

 

3.查询字段c以1开头的行

test=# select * from tbl_insert where c like 1%;
 a | b | c  
---+---+----
 1 | 1 | 11
 6 | 6 | 1
(2 rows)

4.查询字段c中包含字符‘1‘的行

 

test=# select * from tbl_insert where c like %1%;
 a | b |   c   
---+---+-------
 1 | 1 | 11
 5 | 5 | 51
 6 | 6 | 1
 6 | 6 | 61
 6 | 6 | 661
 7 | 7 | 3%1
 8 | 8 | 3%_1
 8 | 8 | 3_%_1
(8 rows)

5.查询字段c中包含下划线‘_‘的行

 

test=# select * from tbl_insert where c like %\_%;
 a | b |   c   
---+---+-------
 8 | 8 | 3%_1
 8 | 8 | 3_%_1
(2 rows)

6.查询字段c中包含百分号‘%‘的行

 

test=# select * from tbl_insert where c like %\%%;
 a | b |   c   
---+---+-------
 7 | 7 | 3%1
 8 | 8 | 3%_1
 8 | 8 | 3_%_1
(3 rows)

 

7.ILIKE查询字段c中包含字符‘b‘(不区分大小写)的行

test=# select * from tbl_insert where c ilike %b%;
 a | b |  c  
---+---+-----
 7 | 7 | abc
 7 | 7 | ABc
 7 | 7 | aBC
(3 rows)

 

二.SIMILAR TO

SIMILAR TO除下划线和百分号的使用与LIKE相同,还支持正则表达式查询。

|   表示选择(二选一,如a|b,类似or)

*   表示重复前面项0次或多次,如‘6*1‘,‘(66)*1‘

+   表示重复前面项1次或多次,如‘6+1‘,‘(66)+1‘

[]  表示方括号内字符集中的任意一个字符,如[123]表示1或2或3中的1个,可以是1,也可以是2,还可以是3,但是只能是单个字符。

 

1.|----查询c字段值是‘abc‘或‘ABc‘的行

test=# select * from tbl_insert where c similar to (ab|AB)c;
 a | b |  c  
---+---+-----
 7 | 7 | abc
 7 | 7 | ABc
(2 rows)

2.*----查询c字段中以‘1‘结尾,前面有0个或多个‘6‘的行

test=# select * from tbl_insert where c similar to 6*1;
 a | b |  c  
---+---+-----
 6 | 6 | 1
 6 | 6 | 61
 6 | 6 | 661
(3 rows)

 

3.*----查询c字段中以‘1‘结尾,前面有0个或多个‘66‘的行

test=# select * from tbl_insert where c similar to (66)*1;
 a | b |  c  
---+---+-----
 6 | 6 | 1
 6 | 6 | 661
(2 rows)

4.+----查询c字段中以‘1‘结尾,前面至少有1个‘6‘的行

test=# select * from tbl_insert where c similar to 6+1;
 a | b |  c  
---+---+-----
 6 | 6 | 61
 6 | 6 | 661
(2 rows)

 

5.+----查询c字段中以‘1‘结尾,前面至少有1个‘66‘的行

test=# select * from tbl_insert where c similar to (66)+1;
 a | b |  c  
---+---+-----
 6 | 6 | 661
(1 row)

 

6.[]----查询字段c中以‘1‘结尾,前面是0到9之间任意一个数字的行

test=# select * from tbl_insert where c similar to [0-9]1;
 a | b | c  
---+---+----
 1 | 1 | 11
 5 | 5 | 51
 6 | 6 | 61
(3 rows)

 

7.[]----查询字段c中以‘1‘结尾,前面是1或6中的行

test=# select * from tbl_insert where c similar to [1,6]1;
 a | b | c  
---+---+----
 1 | 1 | 11
 6 | 6 | 61
(2 rows)

 

postgresql----LIKE和SIMILAR TO

标签:

人气教程排行