sql - 如何将查询与子查询连接起来?
问题描述
所以我是一个完全新手,试图解决这个练习,我必须找到所有标记为素食但成分中含有火鸡的菜肴。
这是我尝试过的(这是我内部加入 3 个表以查找成分的地方):
SELECT Name
FROM Dishes
INNER JOIN DishesIngredients ON DishesIngredients.DishId = s.Id
INNER JOIN Ingredients ON DishesIngredients.IngredientID = Ingredients.ID
这是我似乎无法加入子查询来识别 Vegetarian 标签的地方:
WHERE Ingredients.Name = 'Turkey meat' =
(SELECT Name
FROM Tags
INNER JOIN DishesTags ON DishesTags.TagID = Tags.ID
INNER JOIN Dishes ON DishesTags.DishID = Dishes.ID)
数据库图在这里供参考:
解决方案
您可以使用exists
和子查询:
select d.*
from dishes d
where
exists (
select 1
from dishestags dt
innerjoin tags t on t.id = dt.tagid
where dt.dishid = d.id and t.name = 'Vegetarian'
)
and exists (
select 1
from dishesingredients di
inner join ingredients i on i.id = di.ingredientid
where di.dishid = d.id and i.name = 'Turkey'
)
推荐阅读
- google-cloud-platform - 用于 Minecraft 服务器的 Google VM RAM
- typescript - TypeScript:通用扩展对象,只有可选键
- html - 调整轮播项目的大小
- mysql - 在 mysql 中使用 webscraper 为数据库插入编写触发器......不断收到不同的错误
- javascript - 是的嵌套模式验证
- c++ - 将值传递到数组/数组中的值丢失时出错
- networking - 是否可以知道哪个输入门触发了 Omnet++ 的 handleMessage() 方法?
- powershell - ConvertFrom-Json 为 Powershell 中的某些部分提供 System.Object[]
- python - 如何用图像处理库opencv和python语言提取材料的轮廓(2D绘图)?
- arrays - array.flatten() 末尾有括号 () 但 array.ndim 末尾没有括号????为什么?