H
H
hesy2021-08-27 00:24:06
MySQL
hesy, 2021-08-27 00:24:06

How to sort the result by the number of "caught" id?

I don’t know how else to correctly name the question)

There is a recipe_ingredients table :

+----+-----------+---------------+
| id | recipe_id | ingredient_id |
+----+-----------+---------------+
| 1  | 100       | 500           |
+----+-----------+---------------+
| 2  | 100       | 501           |
+----+-----------+---------------+
| 3  | 200       | 501           |
+----+-----------+---------------+
| 4  | 200       | 900           |
+----+-----------+---------------+
| 5  | 200       | 901           |
+----+-----------+---------------+


How to sort the results by the number of presence of ingredient_id with one recipe_id when selecting (name the field, for example, catch_ingredients )?
SELECT 
  recipe_id, 
  catch_ingredients 
FROM 
  recipe_ingredients
WHERE 
  ingredient_id IN (501, 900) 
GROUP BY 
  recipe_id 
ORDER BY 
  catch_ingredients DESC


To get this result:
+-----------+-------------------+
| recipe_id | catch_ingredients |
+-----------+-------------------+
| 200       | 2                 | <--- Поймали 501 и 900 
+-----------+-------------------+
| 100       | 1                 | <--- Поймали только 501
+-----------+-------------------+


My knowledge of SQL is lacking to implement this.
Most likely, it is not the IN operator that is required here .

I will explain in simple terms what I wanted to do:
There is a conditionally refrigerator with a set of ingredient_id.
You need to get from it all the recipe_id where the ingredient_id occurs (make a list of relevant recipes from the available products), BUT , the recipe does not have to contain all the transferred ingredients (for example, passed onions, potatoes, chicken, sugar, orange, showed recipes with "potato with chicken " and "orange juice")
In the case of ingredient_id IN ... it chooses where all listed ids must be strictly.

recipe_id, ingredient_id have relationships with other tables

ps, I'm sawing a pet project for the sake of interest, but on the main feature I stalled and I'm stupid)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Gordinskiy, 2021-08-27
@DmitriyGordinskiy

SELECT value, COUNT(*)
FROM table
GROUP BY 1
ORDER BY 2 DESC

R
Rsa97, 2021-08-27
@Rsa97

So what is the question?
"Find recipes that include at least one ingredient from the list?

SELECT `recipe_id`, `ingredient_id`
  FROM `recipe_ingredients`
  WHERE `ingredient_id` IN (501, 900)

"The recipe can be prepared from the indicated ingredients"? This means that all the ingredients of this recipe are present in the given list. That is, the total number of recipe ingredients is equal to the number of ingredients of this recipe that are present in the list.
SELECT `recipe_id`, COUNT(*) AS `count`,
    SUM(`ingredient_id` IN (501, 900)) AS `avail`
  FROM `recipe_ingredients`
  GROUP BY `recipe_id`
  HAVING `count` = `avail`

PS
And the `id` field in the table is superfluous. A composite primary key (`recipe_id`, `ingredient_id`) is sufficient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question