2013-04-13 45 views
0

我有這個表:SQL查詢沒有顯示正確的數據

Recipe = (idR, recipeTitle, prepText, cuisineType, mealType) 
Ingredient = (idI, ingrDesc) 
RecipIngr = (idR*, idI*) 

,我試圖顯示IDR和包含的成分蜂蜜或芥末配方標題。這是我的查詢

select idr, recipetitle 
from recipe 
where idr IN (select idr from recpingr where idi = 
(select distinct idr from ingredient where ingrdesc like '%honey%')) 
INTERSECT 
select idr, recipetitle 
from recipe 
where idr IN (select idr from recpingr where idi = 
(select distinct idr from ingredient where ingrdesc like '%mustard%')) 
ORDER BY idr; 

由於某種原因,這將不會顯示正確的數據,我不知道我做錯了什麼。任何幫助?我的查詢有問題嗎?

+0

請告訴我們一些示例數據,您的查詢的結果,你爲什麼會認爲「正確」的數據。理想情況下在http://sqlfiddle.com –

回答

2

像這樣的幫助?

SELECT r.idr, r.recipetitle 
FROM recipe r 
INNER JOIN recipingr ring 
    ON ring.idr = r.idr 
INNER JOIN ingredient ing 
    ON ing.idi = ring.idi 
WHERE ing.ingrdesc LIKE '%honey%' 
OR ingrdesc LIKE '%mustard%' 
ORDER BY r.idr 
2

我想你過度工程它。爲什麼不能是這樣的:

select idr, recipetitle 
from recipe r join recipInbg ri on r.idr = ir.idr 
join ingredient i on ri.idI = i.idI 
where ingrdesc like '%honey%' 
or ingrdesc like '%mustard%' 
1

在這裏你去:

select r.idr, r.recipeTitle from recipe r, ingredient i, recipIngr ri 
    where r.idR=ri.idR and ri.idI=i.idI and 
    (i.ingrDesc like '%honey%' or i.ingrDesc like '%mustard%')