I have query that work fine, but it has 2 the same subqueries
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
I try to optimize with JOIN, but new query returns empty result
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
Than i try variables, but i think i use it wrong, query returns empty result
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
Avoid the dependent subqueries. Instead, have one query which relates products to offer counts. This query you can use in several places. It will most likely be stored temporarily in memory, avoiding duplicate computations, but the database engine might do more clever things as well.
If the relation between P and PS is one-to-one, then you could even work without any subquerys whatsoever:
You also should consider whether there is a good reason to count a specific column. In most cases,
COUNT(*)will be faster thanCOUNT(col). The only reason to use the latter is if you explicitely want to excludeNULLvalues from the count.