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,''),(2,2,''),(3,3,''),(4,4,''),(5,5,''),(6,6,''),(6,6,''),(6,6,''),(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的更多相关文章

  1. PostgreSQL Q&A: Building an Enterprise-Grade PostgreSQL Setup Using Open Source Tools

    转自:https://www.percona.com/blog/2018/10/19/postgresql-building-enterprise-grade-setup-with-open-sour ...

  2. PostgreSQL 之 CREATE FUNCTION

    官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...

  3. 广州PostgreSQL用户会技术交流会小记 2015-9-19

    广州PostgreSQL用户会技术交流会小记 2015-9-19 今天去了广州PostgreSQL用户会组织的技术交流会 分别有两个session 第一个讲师介绍了他公司使用PostgreSQL-X2 ...

  4. postgresql常用命令

    1.createdb 数据库名称 产生数据库2.dropdb 数据库名称 删除数据库 3.CREATE USER 用户名称 创建用户4.drop User 用户名称 删除用户 5.SELECT use ...

  5. 【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM

    source: http://rostislav-matl.blogspot.com/2011/08/fast-inserts-to-postgresql-with-jdbc.html Thanks ...

  6. YUM Installation PostgreSQL

    PostgreSQL can be installed using RPMs (binary) or SRPMs (source) managed by YUM. This is available ...

  7. PostgreSQL configuration file postgresql.conf recommanded settings and how it works

    1        Set max_connections to three times the number of processor cores on the server. Include vir ...

  8. PostgreSQL Partitions

    why we need partitions The first and most demanding reason to use partitions in a database is to inc ...

  9. PostgreSQL中initdb做了什么

    在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库):一起来看一下initdb做了什么吧. 初始化数据库的操作为: ./initdb -D /usr/local/pgsql/dat ...

  10. Understanding postgresql.conf : log*

    After loooong pause, adding next (well, second) post to the “series“. This time, I'd like to describ ...

随机推荐

  1. 【转】Mac下升级python2.7到python3.6

    1. 前言 Mac系统自带python2.7,本文目的是将自带的python升级到3.6版本. 网上有本多的做法是让python2.7和python3.X两个版本共存,博主并不知道,是两版本共存好,还 ...

  2. 初涉RxAndroid结合Glide实现多图片载入操作

    转载请注明出处:王亟亟的大牛之路 本来周末就想发了然后各种拖拉就没有然后了,那么就今天早上写吧,废话不多開始正题 什么是RxJava或者RxAndroid我就不多废话了,理论知识一大堆人给我们做好了. ...

  3. Javascript的setTimeOut和setInterval的定时器用法

    setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则是在每隔指定的毫秒数循环调用函数或表达式, 直到 clearInterval把它清除.也就是说se ...

  4. sudo 之后 unable to resolve host的问题解决办法

    gedit /etc/hosts #127.0.0.1 localhost #127.0.0.1 Masterback或者其他 把后面的Masterback 或者其他改成新的主机名,应该是最近修改过主 ...

  5. 做BS开发,你应该知道的一些东西

    界面和用户体验(Interface and User Experience) 知道各大浏览器执行Web标准的情况,保证你的站点在主要浏览器上都能正常运行.你至少要测试以下引擎:Gecko(用于Fire ...

  6. Oracle收购Apiary来加强其API集成云

        Oracle宣布计划于1月19日收购Apiary,一家专注于API设计和协作的API管理公司.Apiary最为人所知的是API flow,其API管理平台.     Oracle并没有宣布计划 ...

  7. Ubuntu 16.04 获取 root 用户权限并以 root权限登录

    http://blog.csdn.net/csdn_flyyoung/article/details/52966583

  8. 机器学习技法之Aggregation方法总结:Blending、Learning(Bagging、AdaBoost、Decision Tree)及其aggregation of aggregation

    本文主要基于台大林轩田老师的机器学习技法课程中关于使用融合(aggregation)方法获得更好性能的g的一个总结.包含从静态的融合方法blending(已经有了一堆的g,通过uniform:voti ...

  9. ios 调用系统应用的方法 应用间跳转的方法

    声明一个私有方法: #pragma mark - 私有方法 -(void)openUrl:(NSString *)urlStr{ //注意url中包含协议名称,iOS根据协议确定调用哪个应用,例如发送 ...

  10. Linux下gcc编译生成动态链接库*.so文件并调用它(注:执行Test程序后无需用export 命令指定.so库文件路径:方法在文中下方;)

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...