【问题标题】:How do I write a contains query in Postgres that spans two tables?如何在 Postgres 中编写跨越两个表的包含查询?
【发布时间】:2019-08-11 08:56:52
【问题描述】:

我正在使用 Postgres 9.5。我有一张带有label 列的表...

    Table "public.article"
             Column             |           Type           |                           Modifiers                            
--------------------------------+--------------------------+----------------------------------------------------------------
    ...
 label                          | text                     | 

然后我有另一个带有特定单词的表...

mydb=> \d keyword;
                                 Table "public.keyword"
 Column |          Type          |                          Modifiers                           
--------+------------------------+--------------------------------------------------------------
 id     | integer                | not null default nextval('keyword_id_seq'::regclass)
 word   | character varying(200) | not null

如何编写一个查询来检查articlelabel 是否包含(以不区分大小写的方式)来自keyword 表的word 之一?如果这有助于加快速度,我愿意更改每个数据类型。

【问题讨论】:

  • 请分享一些示例数据、预期输出以及您尝试过的内容?

标签: sql postgresql sql-like contains


【解决方案1】:

您可以使用ILIKE 进行不区分大小写的模式匹配:

SELECT a.label
FROM article AS a
   JOIN keyword AS k
      ON a.label ILIKE '%' || l.word || '%';

如果两个表都很大,这会很慢,因为只能使用嵌套循环连接。我认为这是无法避免的。

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 2010-11-11
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多