postgresql - 带有左连接的 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 join
与to_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
我在使用子查询时做到了。按某些单词和类别按全文搜索过滤。但这是正确的做法吗?
解决方案
编码
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
推荐阅读
- ios - 使用内核编写纹理
- javascript - 反应虚拟化动态表格高度
- spring-integration - 如何从标头动态解析处理程序和方法?
- elixir - Phoenix 从 %Plug.Conn{} 远程 ip 解析请求主机
- python - 如何使用 Python 执行 git checkout -q --archive?
- mysql - 内部连接涉及时的 MySQL 条件选择
- reactjs - 如何让 React Web 应用同时在 http 和 https 上运行?
- ionic3 - Ionic HttpClient:JSONObject 文本必须以 '{' 在 1 [字符 2 第 1 行] 开头
- azure-devops - 如何使用 Azure Devops 管道将代码优先的实体框架数据库部署到 SQL 服务器
- javascript - whatwg-fetch SyntaxError:意外的字符串