【发布时间】:2016-08-16 05:10:58
【问题描述】:
我的数据库中有 2 个表 t_recipe 和 t_recipe_ingredient 具有一对多的关系意味着一个食谱可以有多种成分。我必须设置一个过滤条件,它应该给我包含或排除成分的食谱。
对于包含我在下面创建的查询,它工作正常:
select *
from t_recipe r
join t_recipe_ingredient rexc ON r.RecipeID = rexc.RecipeID
where r.RecipeTypeID = 1
and rexc.IngrId in (110, 111)
但是对于排除我得到的食谱有 110,111 但它不应该返回它们,我认为这是由于内部连接也包括所有其他成分和返回食谱:
select *
from t_recipe r
join t_recipe_ingredient rexc WITH (NOLOCK) ON r.RecipeID = rexc.RecipeID
where r.RecipeTypeID = 1
and rexc.IngrId not in (110, 111)
【问题讨论】:
-
设置Bad Habits to kick - putting NOLOCK everywhere - 不建议在任何地方使用它 - 恰恰相反!
-
@marc_s:感谢您格式化查询。我已从查询中删除了 NoLock。现在你能帮我或建议我做错了什么。
标签: sql sql-server join