• 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)
  • 相关阅读:
    取文本中数字
    成绩统计excel
    excel日期转化为周次
    ConcurrentHashMap之实现细节(转)
    线程互斥(互斥变量)
    Spring的历史论(数据脱敏)
    Java之递归
    触摸java常量池
    利用ant脚本 自动构建svn增量/全量 系统程序升级包
    JDK1.5/1.6/1.7新特性
  • 原文地址:https://www.cnblogs.com/alianbog/p/5617079.html
Copyright © 2020-2023  润新知