Question

I have to get products in multiples categories (Prestashop database).

This my query :

SELECT COUNT( cp.`id_product` ) AS total
FROM  `ps_product` p
INNER JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 ) 
LEFT JOIN  `ps_category_product` cp ON p.`id_product` = cp.`id_product` 
WHERE cp.`id_category` =6
AND cp.`id_category` =126
AND product_shop.`visibility` 
IN (
"both",  "catalog"
)
AND product_shop.`active` =1

I want to select inside category 6 AND inside category 126, but my query return 0.

A product can have multiples categories, so i want to select only if products are inside the two categories.

How can this be fixed, so I am getting the expected result?

Was it helpful?

Solution

SELECT COUNT( cp.id_product ) AS total
FROM  ps_product p
Left JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
                                            AND product_shop.id_shop =1 ) 
LEFT JOIN  ps_category_product cp ON p.id_product = cp.id_product 
WHERE cp.id_category in(6,126)
    AND product_shop.visibility IN ('both',  'catalog')
    AND product_shop.active =1

Friend Try this,

SELECT COUNT( cp.id_product ) AS total
FROM  ps_product p
Left JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
                                            AND product_shop.id_shop =1 ) 
LEFT JOIN  ps_category_product cp ON p.id_product = cp.id_product 
            and cp.id_category in(6)
LEFT JOIN  ps_category_product cp1 ON p.id_product = cp1.id_product 
            and cp1.id_category in(126)
WHERE cp.id_category is not null
    and cp1.id_category is not null
    AND product_shop.visibility IN ('both',  'catalog')
    AND product_shop.active =1

OTHER TIPS

Use

WHERE cp.`id_category` =6
OR cp.`id_category` =126

Do u mean between 6 and 126? It is not possible to validate against one column for both 6 and 126. If it is between then use,

SELECT COUNT( cp.`id_product` ) AS total
FROM  `ps_product` p
INNER JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 ) 
LEFT JOIN  `ps_category_product` cp ON p.`id_product` = cp.`id_product` 
WHERE cp.`id_category` between 6 and 126
AND product_shop.`visibility` 
IN (
"both",  "catalog"
)
AND product_shop.`active` =1
WHERE (cp.`id_category` = 6 OR cp.`id_category` = 126)
AND product_shop.`visibility` IN ("both",  "catalog")
AND product_shop.`active` = 1

In both categories:

SELECT COUNT( cp.id_product ) AS total
FROM  ps_product p
Left JOIN ps_product_shop product_shop ON (product_shop.id_product = p.id_product
                                           AND product_shop.id_shop = 1)     
WHERE product_shop.visibility IN ('both',  'catalog')
  AND product_shop.active = 1
  AND EXISTS(SELECT * FROM ps_category_product cp 
             WHERE p.id_product = cp.id_product AND cp.id_category = 6)
  AND EXISTS(SELECT * FROM ps_category_product cp 
             WHERE p.id_product = cp.id_product AND cp.id_category = 12)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top