postgresql----LIKE和SIMILAR TO
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了postgresql----LIKE和SIMILAR TO,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3273字,纯文字阅读大概需要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
标签:
本文系统来源:http://www.cnblogs.com/alianbog/p/5617079.html
内容总结
以上是互联网集市为您收集整理的postgresql----LIKE和SIMILAR TO全部内容,希望文章能够帮你解决postgresql----LIKE和SIMILAR TO所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。