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. 关于Cocos2d-x中掉帧导致游戏一卡一卡的网上一些的解决方法

    方法1 掉帧主要是setpostion引起的  因为每一帧每一个精灵都要set一次虽然不知道为什么会这样但是if(poX<1000&&pox>-100){     xx-& ...

  2. python + opencv: kalman 跟踪

    之前博文中讲解过kalman滤波的原理和应用,这里用一个跟踪鼠标的例程来演示怎么在opencv里用自带的kalman函数进行目标跟踪,文章的内容对做图像跟踪有借鉴意义.文章主要是网络资源进行整理和简单 ...

  3. hibernate中一对多多对一关系设计的理解

    1.单向多对一和双向多对一的区别? 只需要从一方获取另一方的数据时 就使用单向关联双方都需要获取对方数据时 就使用双向关系 部门--人员 使用人员时如果只需要获取对应部门信息(user.getdept ...

  4. QT 交叉编译工具选择

    使用QT交叉编译,生成的都是x86的可执行文件.Zoro告诉我交叉工具配置错了. 参考链接: http://www.cnblogs.com/zengjfgit/p/4744507.html linux ...

  5. C语言中预处理器的相关知识:

    预处理过程时,会做以下事情或着更多: 将所有的#define删除,并且展开所有的宏定义: 处理所有条件编译指令,如#if,#ifdef等: 处理#include预编译指令,将被包含的文件插入到该预编译 ...

  6. spark on yarn(zookeeper 配置)

    http://database.51cto.com/art/201404/435630.htm

  7. Ubuntu 12.04/13.04 安装 Oracle11gR2:该笔记已经陈旧!请参考后续的笔记

    注意点: 在 ubuntu的 /bin 下建立以下几个基本命令的链接: basename awk sh->bash | sh -> ksh 安装以下几个必须的包: binutils bui ...

  8. linux环境,crontab报错Authentication token is no longer valid; new one required You (aimonitor) are not allowed to access to (crontab) because of pam configuration.

    问题描述: 今天同事反应,一个系统上的某些数据没有生成,看了下,怀疑定时任务没有执行,就看下了crontab,发现报了下面的错误: [aimonitor@4A-LF-w08 ~]$ crontab - ...

  9. GIS-009-Cesium 使用

    //加载ArcGIS 发布的地图服务MapServervar url='http://Jason:6080/arcgis/rest/services/SampleWorldCities/MapServ ...

  10. Dubbo(一) -- 初体验

    Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架. 一.Dubbo出现的背景 随着互联网的发展,网站应用的规模不断扩大,常规的 ...