我在我的數據庫中有2個表t_recipe和t_recipe_ingredient具有1對多關係表示一個配方可以有多個成分。我必須把過濾條件,應該給我的食譜,其中包括成分包括或排除在外。SQL Server內部聯接排除結果不起作用
對於包括我在下面的查詢創建工作得很好:
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)
集[壞習慣踢 - 把NOLOCK無處不在(HTTP:// blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) - 不推薦*在任何地方使用它 - 完全相反! –
@marc_s:感謝您格式化查詢。我從查詢中刪除了NoLock。現在你能幫我或者告訴我我做錯了什麼。 – ChupChapCharli