MySQL选择具有多个n到n

编程入门 行业动态 更新时间:2024-10-25 08:21:30
本文介绍了MySQL选择具有多个n到n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些与此类似的表

recipes_tbl | id | Recipe | Directions | ingredients_tbl | id | Ingrediant | Category | recipe_to_ingredient | id | id_recipe | id_ingredient | Quantity

我正在尝试建立一个查询,以获取所有使用牛肉(id_ingredient 1)和土豆(id_ingredient 2)和... n的食谱"

I'm trying to build a query to "Get all recipes that use beef(id_ingredient 1) and potatoes(id_ingredient 2) and ...n"

我怀疑solutios涉及某种花哨的连接...也许吗?

I suspect the solutios involves some kind of fancy joins... maybe?

推荐答案

SELECT R.ID, R.Recipe, R.Directions FROM Ingredients_tbl I INNER JOIN recipe_to_ingredient RI ON I.id = RI.id_ingredient INNER JOIN recipes_tbl R ON R.id = R.id_recipe WHERE I.ID IN (1 ,2) GROUP BY R.ID, R.Recipe, R.Directions HAVING COUNT(*) > 1

应该这样做,尽管本示例中的成分被硬编码为1或2.我需要更多地了解您打算如何提供成分ID,以便在此方面提供更多内容.

Should do it, though the ingredients in this example are hardcoded to only ever be 1 or 2. I'd need to know more about how you intend on supplying the ingredient id's to provide more on that front.

更多推荐

MySQL选择具有多个n到n

本文发布于:2023-10-26 21:58:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!