使用 MySQL 中另一个表的多个条件对不同记录进行计数
这不是家庭作业。我更改了表和字段的名称,仅供说明之用。我承认我对 MySQL 完全陌生。请在您的回答中考虑这一点。
说明我需要的查询功能的最佳方式是这样的:
我有两个表。
一个表与另一表的关系为 0..1 到 0..n。
仅为了简单起见,假设这两个表是配方和成分。
成分表中的字段之一引用配方表,但可能为空。
仅举个例子:
我想知道类似以下内容的 SQL:有多少食谱需要“橄榄”的数量“1”和“蘑菇”的数量为“2”
作为结构化查询语言的新手,我什至不知道如何通过 google 搜索此信息。
我是否走在正确的轨道上?:
SELECT COUNT(DISTINCT Recipe.ID) AS Answer FROM Recipe, Ingredient
WHERE Ingredient.RecipeID=Recipe.ID AND Ingredient.Name='Olives'
AND Ingredient.Amount=1 AND Ingredient.Name='Mushrooms'
AND Ingredient.Amount=2
我意识到这是完全错误的,因为名称不能同时是橄榄和蘑菇......但不知道该放什么,因为我需要计算所有食谱,但只所有食谱都包含两者。
我怎样才能正确地为 MySQL 编写这样的查询?
This Is Not Homework. I have changed the names of the tables and fields, for illustrative purposes only. I admit that I am completely new to MySQL. Please consider that in your answer.
The best way to illustrate the function of the query I need is like this:
I have two tables.
One table has a 0..1 to 0..n relationship to the other table.
For Simplicities Sake Only, Suppose that the two tables were Recipe and Ingredient.
One of the fields in the Ingredient table refers to the Recipe table, but may be null.
Just For Example:
I want to know the SQL for something like: How many recipes call for "Olives" in the amount of "1" AND "Mushrooms" in the amount of "2"
Being brand new to The Structured Query Language, I'm not even sure what to google for this information.
Am I on the right track with the following?:
SELECT COUNT(DISTINCT Recipe.ID) AS Answer FROM Recipe, Ingredient
WHERE Ingredient.RecipeID=Recipe.ID AND Ingredient.Name='Olives'
AND Ingredient.Amount=1 AND Ingredient.Name='Mushrooms'
AND Ingredient.Amount=2
I realize this is totally wrong because Name cannot be BOTH Olives And Mushrooms... but don't know what to put instead, since I need to count all recipes with both, but only all recipes with both.
How can I properly write such a query for MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你很接近。
尝试类似的方法
您可以两次加入同一个表,您所需要做的就是为表提供适当的 别名。
You were close.
Try something like
You can join to the same table twice, all you need to do is give the table an appropriate alias.
使用:
Use:
希望这有帮助
Hope this help
首先,您使用旧的连接语法。 INNER JOIN 创建相同的执行计划,但更加清晰。然后,您的查询都是关于简单的旧条件。你只需要把它们写正确就可以了! (我同意,这需要一些练习。)
这是我的示例数据:
我的查询输出 2,正如它应该的那样。
编辑:实际上,我意识到我的查询选择了错误的行。这工作正常:
它也输出 2。
First of all, you use the old join syntax. INNER JOIN creates the same execution plan, but is much more clear. Then, your query is all about a plain, old condition. You just have to write them correctly! (It takes some practice, I agree.)
Here is my sample data :
And my query outputs 2, as it should.
Edit: Actually, I realised my query selected wrong rows. This works correctly :
Which outputs 2 as well.
仅供参考...
有多少食谱需要“1”份量的“橄榄”和“2”份量的“蘑菇”
查看上面的表格结构,您可能想要一个关联实体在食谱和配料之间。这使得使用内部查询来执行您正在寻找的查询变得更加容易加入。
在你的两张桌子之间,我会想象这样的东西......
如果你有这样的东西,那么每个食谱可能有很多成分,并且每种成分可能是许多食谱的一部分。一旦您有了这样的表格来正确关联两个表格,您就可以将它们连接在一起并获得完整的配方。对于像这样的更复杂的查询,其中两个食谱有单独的条件,我可能会对其进行子查询以帮助理解。
Just for reference...
How many recipes call for "Olives" in the amount of "1" AND "Mushrooms" in the amount of "2"
Looking at your table structure above, you may want an associative entity to go between recipes and ingredients. This makes it a lot easier to do the kind of query you're looking for with an inner join.
In between your two tables, I would imagine something like this...
If you have that, then each recipe may have many ingredients, and each ingredient may be a part of many recipes. Once you have a table like that to properly associate your two tables, you can join them together and get a complete recipe. For a more complicated query like this where you have separate conditions for two recipes, I would probably sub-query it to help understanding.