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,''),(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的更多相关文章
- 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 ...
- PostgreSQL 之 CREATE FUNCTION
官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...
- 广州PostgreSQL用户会技术交流会小记 2015-9-19
广州PostgreSQL用户会技术交流会小记 2015-9-19 今天去了广州PostgreSQL用户会组织的技术交流会 分别有两个session 第一个讲师介绍了他公司使用PostgreSQL-X2 ...
- postgresql常用命令
1.createdb 数据库名称 产生数据库2.dropdb 数据库名称 删除数据库 3.CREATE USER 用户名称 创建用户4.drop User 用户名称 删除用户 5.SELECT use ...
- 【转载】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 ...
- YUM Installation PostgreSQL
PostgreSQL can be installed using RPMs (binary) or SRPMs (source) managed by YUM. This is available ...
- 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 ...
- PostgreSQL Partitions
why we need partitions The first and most demanding reason to use partitions in a database is to inc ...
- PostgreSQL中initdb做了什么
在使用数据库前,是启动数据库,启动数据库前是initdb(初始化数据库):一起来看一下initdb做了什么吧. 初始化数据库的操作为: ./initdb -D /usr/local/pgsql/dat ...
- Understanding postgresql.conf : log*
After loooong pause, adding next (well, second) post to the “series“. This time, I'd like to describ ...
随机推荐
- Qt 线程基础(QThread、QtConcurrent等)
[-] 使用线程 何时使用其他技术替代线程 应该使用 Qt 线程的哪种技术 Qt线程基础 QObject与线程 使用互斥量保护数据的完整 使用事件循环防止数据破坏 处理异步执行 昨晚看Qt的Manua ...
- (转)关于三星cortex A9 Sate4412 开发板 uboot 启动的一些问题释疑
说明:本文转载自:http://blog.csdn.net/gooogleman/article/details/17238079 作者:gooogleman 日 ...
- 关于多层for循环迭代的效率优化问题
关于多层for循环迭代的效率优化问题 今天笔试的时候遇到这么一道题目 说有上面这么循环嵌套 .问怎么优化 并说明原因. for(int i = 0 ; i < 1000 ;i++){ ...
- 扫盲:java中关于路径的问题
../FileName:当前工程的上级目录. ./FileName:当前工程所在的目录. /FileName:当前工程所在磁盘的根目录(windows下). FileName:当前工程所在的目录.
- C语言对文件的操作函数用法详解1
在ANSIC中,对文件的操作分为两种方式,即: 流式文件操作 I/O文件操作 一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下: typedef str ...
- Storm-源码分析-Streaming Grouping (backtype.storm.daemon.executor)
executor在发送outbounding message的时候, 需要决定发送到next component的哪些tasks 这里就需要用到streaming grouping, 1. mk- ...
- linux用户态定时器的使用---19
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ linux操作系统为每一个进程提供了3个内部计时器. ITIMER_REAL;ITIMER_VI ...
- 使用 MVVMLight 绑定数据
如果你还不知道如何在VS中加入MVVMLight的引用,那么建议你先翻阅这篇文章:在VS中安装/使用 MVVMLight 这篇文章主要是介绍如何使用MVVMLight来绑定数据到界面中(View),以 ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- Java 基本语法----进制、运算符
进 制 对于整数,有四种表示方式: 二进制:0,1 ,满 2 进 1.以 0b 或 0B 开头.十进制:0-9 ,满 10 进 1. 八进制:0-7 ,满 8 进1. 以数字 0 开头表示.十六进制: ...