繁体   English   中英

从表中输出多个结果

[英]Outputting multiple results from tables

配方数据库继续...

分类

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits

食谱

id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette

配料

iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range

recipe_categories

recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3

配方成分

recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5

如ozatomic所建议,我正在使用以下查询:

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 = <search_id>
GROUP BY id

问题是,我意识到我将需要在成分表中添加其他字段,例如Ingredient_code和Ingredient_brand,这意味着我无法使用GROUP_CONCAT列出成分,因为我需要将以下内容输出到Web(仍然按类别):

black forest cake:
abc201  white wings   self-raising flour
abc202  pura          milk
abc203  plaistock     chocolate

croquembouche:
abc201  white wings   self-raising flour
abc204  white wings   plain flour

crepes suzette:
abc202  pura          milk
abc205  white wings   plain flour
abc206  free range    eggs 

我感觉我要么必须使用两个查询来实现此目的,要么使用foreach循环。 哪个最好,我将如何处理?

编辑:我还需要能够格式化结果,使用html标签或放入divs或其他。

我昨天在您询问时给出答案仍然适用。 :)

确实,您将必须汇总一个列表并遍历整个列表,以提取所需的确切数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM