خرید بک لینک
Hello, I am a newbie in MySQL and I need some help selecting from a one to many relationship table. I have this table:
recipe_ingredients
+-----------+---------------+
| recipe_id | ingredient_id |
+-----------+---------------+
| 1 | 1 |
| 1 | 4 |
| 2 | 1 |
| 2 | 4 |
| 2 | 2 |
| 3 | 1 |
| 3 | 3 |
| 3 | 4 |
| 3 | 2 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
+-----------+---------------+


So I want to select recipe_id by ingredient_id's. But it should only select recipe_id's for which I have specified all the ingredients. For example if I input ingredient_id's (1,2,3) it should select recipe_id: 4, or If I input (1,2,3,4) It should select recipe_id: 1,2,3,4. After some googling I found this query:
Code:

SELECT DISTINCT recipe_id FROM recipe_ingredients WHERE ingredient_id IN(1,2) GROUP BY recipe_id HAVING COUNT(ingredient_id)=2;
However this does not work as needed.
If you could point me to some tutorials or where I could learn how to do something similar to this I would appreciate it.

برچسب: نویسنده: آیلین رضوانی تاريخ: يکشنبه 17 دی 1396 ساعت: 2:24

صفحه بندی