简体   繁体   English

这什么也不会返回。 为什么?

[英]This returns nothing. Why?

SELECT 
    Recipes.RecipeID, Recipes.RecipeTitle
FROM 
    Recipes 
INNER JOIN
    Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID 
INNER JOIN
    Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID
WHERE 
   (Ingredients.IngredientName = 'Beef') 
   AND (Ingredients.IngredientName = 'Garlic')

This SQL query returns nothing. 此SQL查询不返回任何内容。 However when I checked the where conditions individually/ separately without putting them together with AND, they come up with a recipe called "Roast Beef" that actually has both beef and garlic. 但是,当我单独/单独检查条件时,没有将它们与AND放在一起时,他们想出了一个叫“烤牛肉”的食谱,实际上含有牛肉和大蒜。 Hence, shouldn't it show up as 1 row in the result. 因此,它不应该在结果中显示为1行。 But it doesn't show anything. 但是它什么也没显示。 Why? 为什么?

It is returning NULL , because an ingredient cannot have to names at the same time. 它返回NULL ,因为一个成分不必同时命名。 You would seem to want: 您似乎想要:

SELECT r.RecipeID, r.RecipeTitle
FROM Recipes r INNER JOIN
     Recipe_Ingredients ri
     ON r.RecipeID = ri.RecipeID INNER JOIN
     Ingredients i
     ON i.IngredientID = ri.IngredientID
WHERE i.IngredientName IN ('Beef', 'Garlic')
GROUP BY r.RecipeID, r.RecipeTitle
HAVING COUNT(DISTINCT i.IngredientName) = 2;

AND operates at row level. AND在行级别运行。 The IngredientName can't be both 'Beef' and 'Garlic' at the same time for the same row. 对于同一行,IngredientName不能同时是'Beef'和'Garlic'。 You may have to join with Ingredients again. 您可能需要重新加入配料。

you can't use AND operator for multi condition in same column, if you want to select 2 condition for same column use IN operator. 如果要为同一列选择2个条件,则不能在同一列中使用AND运算符。

SELECT Recipes.RecipeID, Recipes.RecipeTitle
FROM Recipes INNER JOIN
Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID 
INNER JOIN
Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID
WHERE (Ingredients.IngredientName in ('Beef' , 'Garlic') ) 

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

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