duannaikuang1301 2010-07-21 00:00
浏览 24
已采纳

从表中输出多个结果

The recipe db continues...

categories

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

recipes

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

ingredients

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_ingredients

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

As suggested by ozatomic, I'm using the following query:

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

Problem is, I've realised I'm going to need additional fields in the ingredients table, such as ingredient_code and ingredient_brand, which will mean that I'm unable to use GROUP_CONCAT to list ingredients as I need the following outputted to web (still by category):

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 

I get the feeling that I'm either going to have to use two queries to achieve this, or use a foreach loop. Which would be best and how would I go about it?

edit: I also need to be able to format the results, using html tags or putting in divs or whatever.

  • 写回答

1条回答 默认 最新

  • dongmao7195 2010-07-21 00:13
    关注

    The answer I gave when you asked this yesterday still applies. :)

    You are indeed going to have to put together a list and loop over it to pull out the exact data you're looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?