L
L
lolka022020-05-15 11:00:04
MySQL
lolka02, 2020-05-15 11:00:04

How to rewrite the request?

Have a request

SELECT `product`.`id`, `product`.`price`, 
FROM `product` 
LEFT JOIN `product_to_category` `p2c` ON p2c.product_id=product.id 
INNER JOIN `services_product` `sp` ON product.id=sp.product_id 
WHERE  (`p2c`.`category_id` IN ('32', '33', '34', '35', '54', '47', '49', '50')) 
AND (`sp`.`service_id` IN ('90', '92')) 
GROUP BY `sp`.`product_id` AND (COUNT(sp.product_id) =2)


In the condition COUNT(sp.product_id) =2 means that both services 90 and 92 are for the product
due to the fact that the categories of the product are allowed 2 and services 2, then COUNT(sp.product_id) equals 4, how to rewrite so that the categories are not counted? in general, the request is correctly formed)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Gordinskiy, 2020-05-15
@lolka02

It is necessary to count not the product_id in the group, but the unique service_id
... HAVING (COUNT(DISTINCT('sp.service_id'))) ...

L
Lazy @BojackHorseman MySQL, 2020-05-15
Tag

...COUNT(DISTINCT ...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question