postgresql----LIKE和SIMILAR TO


LIKE和SIMILAR TO都支持模糊查詢,另外SIMILAR TO還支持正則表達式查詢。模糊查詢中有兩個重要的符號:下划線'_'匹配任意單個字符,百分號'%'匹配任意多個字符,可以是0個,如果想匹配'_'和'%',必須在'_'和'%'前使用反斜線(\)逃逸。另外和LIKE相似的還有ILIKE,區別是LIKE區分大小寫,而ILKIE不區分大小寫。

 

示例表:

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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM