Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Abfrage mehrerer Tabellen

Abfrage:

SELECT A.recipe_name, GROUP_CONCAT(ingredient_name) AS ingredient_names
FROM recipes A
LEFT JOIN recipe_ingredients B ON A.id = B.recipe_id
LEFT JOIN ingredients C ON B.ingredient_id = C.iid
LEFT JOIN recipe_categories D ON A.id = D.recipe_id
LEFT JOIN categories E ON D.category_id = E.cid
WHERE category_id = <serach_id>
GROUP BY id

Ergebnis:

+-------------------+-------------------------------------------------+
| recipe_name       | ingredient_names                                |
+-------------------+-------------------------------------------------+
| black forest cake | chocolate,baking powder,self-raising flour,milk |
| angel cake        | self-raising flour,milk,chocolate               |
| melting moments   | milk,plain flour                                |
| croquembouche     | self-raising flour,plain flour                  |
| crepes suzette    | milk                                            |
+-------------------+-------------------------------------------------+

Hier ist, wonach Sie gefragt haben, indem Sie GROUP_CONCAT verwendet haben. Jede Zutat wird durch , getrennt