如何在 Postgres 中编写跨两个表的包含查询?

How do I write a contains query in Postgres that spans two tables?

我正在使用 Postgres 9.5。我有一个带有 label 列的 table ...

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

然后我有另一个 table 有特定的词...

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 是否包含(以不区分大小写的方式)来自 [=16] 的 word 之一=] table?如果这有助于加快速度,我愿意更改每个数据类型。

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

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

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