SQL查询未显示正确的数据

时间:2013-04-13 16:34:15

标签: sql contains

我有这张桌子:

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;

由于某些原因,这将无法显示正确的数据,我不知道我做错了什么。有帮助吗?我的查询有问题吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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%'

答案 2 :(得分: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%')