繁体   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')

此SQL查询不返回任何内容。 但是,当我单独/单独检查条件时,没有将它们与AND放在一起时,他们想出了一个叫“烤牛肉”的食谱,实际上含有牛肉和大蒜。 因此,它不应该在结果中显示为1行。 但是它什么也没显示。 为什么?

它返回NULL ,因为一个成分不必同时命名。 您似乎想要:

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在行级别运行。 对于同一行,IngredientName不能同时是'Beef'和'Garlic'。 您可能需要重新加入配料。

如果要为同一列选择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