Answer the question
In order to leave comments, you need to log in
How to correctly formulate a MySQL query?
Hello! There are the following two tables category and special, you need to do the following:
Select all ids from the category table that have all product_ids in the special table.
Category
ID | product_id | description
1 | 123 |
1 | 124 |
1 | 125 |
2 | 126 |
3 | 127 |
4 | 128 |
4 | 129 |
special
product_id | price | old_price
123 | 1 | 1
124 | 1 | 1
125 | 1 | 1
129 | 1 | one
Answer the question
In order to leave comments, you need to log in
SELECT DISTINCT `c`.`id`
FROM `Category` AS `c`
LEFT JOIN (
SELECT DISTINCT `c1`.`id` AS `id`
FROM `Category` AS `c1`
LEFT JOIN `Special` AS `s1` ON `s1`.`Product_id` = `c1`.`Product_id`
WHERE `s1`.`Product_id` IS NULL
) AS `s` ON `s`.`id` = `c`.`id`
WHERE `s`.`id` IS NULL
SELECT `id` from `category` WHERE `product_id` IN (SELECT `product_id` FROM `special`)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question