R
R
Roman Kuzmenko2016-03-02 16:42:47
PHP
Roman Kuzmenko, 2016-03-02 16:42:47

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

2 answer(s)
R
Rsa97, 2016-03-02
@devrvk

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

D
Dmitry Voronkov, 2016-03-02
@DmitryVoronkov

SELECT `id` from `category` WHERE `product_id` IN (SELECT `product_id` FROM `special`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question