【发布时间】:2015-02-15 00:27:28
【问题描述】:
我有以下表格。
Table Name: Recipe
id topic description difficultylevel totaltime
1 Cheese Cheese Easy 10
2 Cheese1 Cheese1 Medium 50
Table Name: Ingredients
id recipeid ingredient
1 1 Butter
2 1 Cheese
3 1 Bread
现在,当我运行以下查询时,它会返回菜谱 id = "1",尽管它不应该返回,因为我不想要其中有黄油的菜谱。
SELECT recipe.id
FROM
`recipe`
INNER JOIN ingredients ON recipe.id = ingredients.recipeid
WHERE
(recipe.description LIKE '%CHEESE%' OR recipe.topic LIKE '%CHEESE%')
AND (recipe.difficultylevel='Easy')
AND (recipe.totaltime <= 30)
AND (ingredients.ingredient <> 'Butter')
由于 Cheese and Bread,它两次返回 recipe.id = "1"。我需要修复查询,以便它完全排除 recipe.id = "1" 如果它有黄油(例如)
【问题讨论】:
-
感谢@Michael 编辑问题。看起来更容易阅读。我真的不知道该怎么做。
-
select distinct?至于格式化您的代码,只需在编辑器中突出显示它并点击{}按钮。 -
使用突出显示代码上的
{}编辑器工具栏按钮或ctl-k格式化代码块。 -
感谢有关如何格式化的帮助。 SELECT DISTINCT 将不起作用,因为它仍然会返回一次 recipe.id = "1" 它根本不应该
-
您得到 1 的回报,因为当成分是奶酪或面包时,您的 where 子句为真。您需要一种不同的方法。
标签: mysql join where-clause