MySQL:查询以获取使用所有给定成分的食谱

发布于 2024-09-05 06:23:20 字数 884 浏览 12 评论 0原文

我有以下简化表:

CREATE TABLE recipe(id int, name varchar(25));  
CREATE TABLE ingredient(name varchar(25));  
CREATE TABLE uses_ingredient(recipe_id int, name varchar(25));

我想要进行一个查询,返回包含鸡肉和奶油的食谱的所有 id。

我尝试过,

SELECT recipe_id FROM uses_ingredient INNER JOIN  
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream")  
USING (name) GROUP BY recipe_id  
HAVING COUNT(recipe_id) >= (SELECT COUNT(*) FROM theme);  

这给了我:“错误1248(42000):每个派生表必须有自己的别名” 并且可能也是错误的。

接下来我尝试

SELECT recipe_id FROM 
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream") AS t 
INNER JOIN uses_ingredient USING (name) 
GROUP BY recipe_id HAVING 
COUNT(recipe_id)>= (SELECT COUNT(*) FROM t);

给出“错误1146(42S02):表'recipedb.t'不存在”

我想避免创建临时表,包括使用ENGINE = MEMORY。

I have the following simplified tables:

CREATE TABLE recipe(id int, name varchar(25));  
CREATE TABLE ingredient(name varchar(25));  
CREATE TABLE uses_ingredient(recipe_id int, name varchar(25));

I want to make a query that returns all id's of recipes that contain both Chicken and Cream.

I have tried

SELECT recipe_id FROM uses_ingredient INNER JOIN  
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream")  
USING (name) GROUP BY recipe_id  
HAVING COUNT(recipe_id) >= (SELECT COUNT(*) FROM theme);  

which gives me :"ERROR 1248 (42000): Every derived table must have its own alias"
and is probably wrong too.

Next I tried

SELECT recipe_id FROM 
(SELECT * FROM ingredient WHERE name="Chicken" OR name="Cream") AS t 
INNER JOIN uses_ingredient USING (name) 
GROUP BY recipe_id HAVING 
COUNT(recipe_id)>= (SELECT COUNT(*) FROM t);

which gives "ERROR 1146 (42S02): Table 'recipedb.t' doesn't exist"

I want to avoid creating temporary tables including using ENGINE=MEMORY.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲念泪 2024-09-12 06:23:20

可能有更好的思考方式,但我认为答案是两组的交集:那些含有鸡肉的食谱和那些含有奶油的食谱。如果您简化的不仅仅是列数,这可能对您不起作用。但我尝试了一些记录,看起来不错。

SELECT CH.Recipe_Id
FROM
  (SELECT Recipe_Id
  FROM Uses_Ingredient
  WHERE Name = 'Chicken') CH
INNER JOIN 
  (SELECT Recipe_Id
  FROM Uses_Ingredient
  WHERE Name = 'Cream') CR
  ON CR.Recipe_Id = CH.Recipe_Id

There is likely a better way to think of it, but I see the answer as the intersection of two sets: those recipes that have chicken in them and those recipes that have cream in them. This might not work for you if you simplified more than just the number of columns. But I tried it with a handful of records and it seems fine.

SELECT CH.Recipe_Id
FROM
  (SELECT Recipe_Id
  FROM Uses_Ingredient
  WHERE Name = 'Chicken') CH
INNER JOIN 
  (SELECT Recipe_Id
  FROM Uses_Ingredient
  WHERE Name = 'Cream') CR
  ON CR.Recipe_Id = CH.Recipe_Id
黯然 2024-09-12 06:23:20

我同意 MJB 的观点;它是两个集合的交集。所以我就买了两套,看看里面都有什么......

SELECT *
    FROM recipe
    WHERE EXISTS (SELECT *
                      FROM uses_ingredient
                      WHERE uses_ingredient.recipe_id = recipe.recipe_id AND
                            uses_ingredient.name      = "Chicken") AND
          EXISTS (SELECT *
                      FROM uses_ingredient
                      WHERE uses_ingredient.recipe_id = recipe.recipe_id AND
                            uses_ingredient.name      = "Cream");

I agree with MJB; it is the intersection of two sets. So I'd just get the two sets and see what's in both of them...

SELECT *
    FROM recipe
    WHERE EXISTS (SELECT *
                      FROM uses_ingredient
                      WHERE uses_ingredient.recipe_id = recipe.recipe_id AND
                            uses_ingredient.name      = "Chicken") AND
          EXISTS (SELECT *
                      FROM uses_ingredient
                      WHERE uses_ingredient.recipe_id = recipe.recipe_id AND
                            uses_ingredient.name      = "Cream");
装迷糊 2024-09-12 06:23:20

其他建议都很好,但这里有另一种方法。这实际上看起来像是您在查询中尝试执行的操作。虽然我不知道为什么当“uses_ingredient”中似乎包含相同的信息(名称)时,您会有“成分”表。

SELECT recipe_id, count(*) c FROM uses_ingredient
WHERE name="Chicken" OR name="Cream"
GROUP BY recipe_id
HAVING c=2

The other suggestions are good, but here is another way. This actually seems like what you were trying to do in your query. Although I don't know why you have and "ingredient" table when the same information (name) seems to be contained in "uses_ingredient".

SELECT recipe_id, count(*) c FROM uses_ingredient
WHERE name="Chicken" OR name="Cream"
GROUP BY recipe_id
HAVING c=2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文