首页 > 解决方案 > 带有左连接的 Postgresql @@ 查询面临错误

问题描述

我在 postgres 中使用 textsearch-controls 进行全文搜索。一切正常。我创建了索引并在查询中使用。但是当我需要加入另一个表时,我遇到了错误。

> ERROR:  invalid reference to FROM-clause entry for table "p"
  LINE 10:             ON cp.product_id = p.id          
                                          ^
  HINT:  There is an entry for table "p", but it cannot be referenced from this part of the query.

我的查询

SELECT 
    p.id AS id,
    p.sku As sku,
    cp .category_id,
    ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank                        
FROM products p, to_tsquery('Urbanears:*') query
LEFT JOIN category_product cp 
        ON cp.product_id = p.id                                     
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query                                                                                   
ORDER BY rank DESC

没有left join查询,@@ query工作正确,没有问题,没有@@ query查询,left join工作正确。我错过了什么?如何left jointo_tsquery和一起使用@@ query

当我在查询后向左移动 jpin 时,我遇到了另一个静默错误

询问

    SELECT 
p.id AS id,
p.sku As sku,
cp .category_id,
ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank
FROM products p, to_tsquery('Urbanears:*') query
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query                                       
LEFT JOIN category_product cp 
ON cp.product_id = p.id

ORDER BY rank DESC  

错误:

> ERROR:  syntax error at or near "LEFT"
  LINE 11:           LEFT JOIN category_product cp 
                     ^

> Time: 0.001s

更新:

SELECT 
b.category_id, a.id, a.rank

FROM (SELECT 
p.id AS id,
ts_rank_cd(to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')), query) AS rank
FROM products p, to_tsquery('Urbanears:*') query
WHERE to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query           

ORDER BY rank DESC) as a 


LEFT JOIN category_product b on b.product_id=a.id
WHERE b.category_id = 181

我在使用子查询时做到了。按某些单词和类别按全文搜索过滤。但这是正确的做法吗?

标签: postgresqlfull-text-search

解决方案


编码

FROM X, Y LEFT JOIN Z ON ...

被解释为

FROM X, (Y LEFT JOIN Z ON ...)

对 X 的引用在 ON 内部无效,因为 ON 仅适用于 Y 和 Z。

你不应该混合逗号连接和显式连接,让它们都像这样显式:

FROM products p JOIN to_tsquery('Urbanears:*') query ON to_tsvector('english',name||' '||coalesce(description,'')||' '||coalesce(sku,'')||' '||coalesce(price,0)||' '||coalesce(category,'')||' '||coalesce(brand,'')) @@ query 
LEFT JOIN category_product cp ON cp.product_id = p.id                                     

推荐阅读