我正在尝试离开加入查询,但它失败并显示以下消息:
未知栏' seller_product.seller_product_id'在' on条款'
以下是查询:
SELECT sp.seller_product_id FROM seller_product
LEFT JOIN (
SELECT spov.seller_product_id
FROM seller_product_option_value spov
WHERE spov.active = 1
AND spov.seller_product_id IN ( 2567 ) ) AS option_query
ON seller_product.seller_product_id = option_query.spov.seller_product_id
答案 0 :(得分:0)
您需要为表格添加别名,请尝试以下操作:
SELECT sp.seller_product_id FROM seller_product sp
LEFT JOIN (
SELECT spov.seller_product_id
FROM seller_product_option_value spov
WHERE spov.active = 1
AND spov.seller_product_id IN ( 2567 ) ) AS option_query
ON sp.seller_product_id = option_query.spov.seller_product_id
答案 1 :(得分:0)
不要嵌套左连接。您可以在ON
子句中进行过滤:
SELECT sp.seller_product_id
FROM seller_product sp LEFT JOIN
seller_product_option_value spov
ON sp.seller_product_id = spov.seller_product_id AND
spov.active = 1 AND
spov.seller_product_id IN ( 2567 );
在MySQL中避免使用FROM
子句中的子查询尤为重要,因为它实现了子查询。这增加了开销并排除了JOIN
的索引的使用。