AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 185454
Accepted
Roots
Roots
Asked: 2017-09-09 11:13:05 +0800 CST2017-09-09 11:13:05 +0800 CST 2017-09-09 11:13:05 +0800 CST

SQL 正则表达式 PostgreSQL

  • 772

让我们考虑这样一个表:

id |            name
 1 | This is a test sentence
 2 | is a sentence TeST
 3 | This a IS test sentence
 4 | iS a tEst sentence tHis
 5 | This a test sentence is

我怎样才能得到所有的行,搜索“是一个句子测试”?

我在用:

SELECT *
FROM table
WHERE name ILIKE '%is%' AND name ILIKE '%a%' AND name ILIKE '%sentence%' AND name ILIKE '%test%'

但这似乎不是最好的方法。

postgresql string
  • 2 2 个回答
  • 399 Views

2 个回答

  • Voted
  1. Evan Carroll
    2017-09-09T11:31:44+08:002017-09-09T11:31:44+08:00

    你有LIKE和ILIKE和。这涵盖了 SQL-LIKE 条件语句和更强大、更强大的类似正则表达式的功能。~~*

    SELECT
     s,
     s LIKE '%is a sentence test%' as "like",
     s ILIKE '%is a sentence test%' AS "ilike",
     s ~ '.*is a sentence test.*' AS "~",
     s ~* '.*is a sentence test.*' AS "~*"
    FROM ( VALUES
     ( 'This is a test sentence' ),
     ( 'is a sentence TeST' ),
     ( 'This a IS test sentence' ),
     ( 'iS a tEst sentence tHis' ),
     ( 'This a test sentence is' )
    ) AS t(s);
    
                s            | like | ilike | ~ | ~* 
    -------------------------+------+-------+---+----
     This is a test sentence | f    | f     | f | f
     is a sentence TeST      | f    | t     | f | t
     This a IS test sentence | f    | f     | f | f
     iS a tEst sentence tHis | f    | f     | f | f
     This a test sentence is | f    | f     | f | f
    (5 rows)
    

    您没有使用任何高级正则表达式功能,所以我会选择LIKE, 或ILIKE。

    • 1
  2. Best Answer
    indiri
    2017-09-09T14:48:51+08:002017-09-09T14:48:51+08:00

    首先,您使用 split_to_array 函数将字符串放入数组 ( https://www.postgresql.org/docs/9.6/static/functions-string.html ),然后您可以比较它们。Postgres 有一个很酷的函数来检查一个数组是否包含另一个数组。有关 Postgres 数组的更多信息,请访问:https ://www.postgresql.org/docs/9.5/static/functions-array.html

    表格设置:

    CREATE TABLE stringchecks
    (
        id serial,
        name varchar(100)
    );
    
    INSERT INTO stringchecks (name) VALUES ('This is a test sentence');
    INSERT INTO stringchecks (name) VALUES  ('is a sentence TeST');
    INSERT INTO stringchecks (name) VALUES ('This a IS test sentence');
    INSERT INTO stringchecks (name) VALUES ('iS a tEst sentence tHis');
    INSERT INTO stringchecks (name) VALUES ('This a test sentence is');
    

    检查匹配你有两个选择。您可以使用 regexp_split_to_array:

    SELECT
        id,
        name,
        regexp_split_to_array(lower(name), E'\\s+') @> 
           regexp_split_to_array(lower('is a sentence test'), E'\\s+') 
    FROM stringchecks;
    

    或者您可以使用 string_to_array:

    SELECT
        id,
        name,
        string_to_array(lower(name), ' ') @> 
           string_to_array(lower('is a sentence test'), ' ')
    FROM stringchecks;
    

    在这种情况下,因为它是一个简单的定界符,所以使用 string_to_array 会更快。http://www.postgresonline.com/journal/archives/370-regexp_split_to_table-and-string_to_array-unnest-performance.html

    • 1

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve