我的查询工作正常,但它有 2 个相同的子查询
SELECT *,
(SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id) AS poffers
FROM `product` P
JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14))
WHERE P.deleted is NULL
AND (SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id) > 0
我尝试使用 JOIN 进行优化,但新查询返回空结果
SELECT *, OJ.pcount AS poffers
FROM `product` P
JOIN product_section PS ON PS.product_id = P.id AND PS.section_id IN (14)
JOIN (SELECT count(O.id) AS pcount, O.product_id FROM `offer` O )
AS OJ ON OJ.product_id = P.id
WHERE P.deleted is NULL AND OJ.pcount > 0
比我尝试变量,但我认为我用错了,查询返回空结果
SELECT *,
@o := (SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id)
AS poffers
FROM `product` P
JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14))
WHERE P.deleted is NULL
AND @o > 0