Postgresql @@查询遇到左连接错误

问题描述 投票:0回答:1

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

> 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查询工作正常,没有问题;没有@@ queryleft 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

UPDATE:

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

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

postgresql full-text-search
1个回答
0
投票

代码

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                                     
© www.soinside.com 2019 - 2024. All rights reserved.