sql - (SQL ZOO) 朱莉·安德鲁斯电影中的男主角
问题描述
SQL Zoo https://sqlzoo.net/wiki/More_JOIN_operations No. 12: 朱莉安德鲁斯电影的男主角 列出所有“朱莉安德鲁斯”出演的电影的片名和男主角。
你有没有得到“马克小小姐两次”?朱莉·安德鲁斯出演了 1980 年翻拍的马克小姐,而不是原版(1934 年)。
标题不是唯一字段,在您的子查询中创建一个 ID 表(这是什么意思?)
我写的是这个,但它说我错了,不知道为什么:
SELECT title, name
FROM (movie JOIN casting ON movie.id = casting.movieid) JOIN actor ON actor.id = casting.actorid
WHERE title IN
(SELECT title FROM casting JOIN actor ON casting.actorid = actor.id
where actor.name = 'Julie Andrews')
AND ord = 1
解决方案
WHERE title IN
(SELECT title FROM casting JOIN actor ON casting.actorid = actor.id
where actor.name = 'Julie Andrews')
或中都没有title
列。所以你不小心关联了这个子查询 - 标题的范围来自子查询之外,所以你实际上有类似的东西casting
actor
where 1 in
(select 1
from casting JOIN actor ON casting.actorid = actor.id
where actor.name = 'Julie Andrews'
)
如果该子查询返回任何行,这将是真的。始终清楚列应该来自哪个行源。
您可能打算从中选择movieid
列casting
并将其用作movie.id
与第一个子查询中的连接类似的过滤器:
SELECT movie.title, actor.name
FROM movie
JOIN casting ON movie.id = casting.movieid
JOIN actor ON actor.id = casting.actorid
WHERE movie.id IN
(SELECT casting.movieId FROM casting JOIN actor ON casting.actorid = actor.id
where actor.name = 'Julie Andrews')
AND casting.ord = 1
推荐阅读
- javascript - 酶仍然在 React 中找到应该通过条件渲染隐藏的元素
- linux - 如何将脏 fastq 文件排序为交错的 fastq
- c# - 在单独的线程中同步更新属性
- php - Uniswap:如何使用 API 读取代币价格
- c# - DSharpPlus - 现有消息的 WaitForReaction
- php - PHP 页面未加载(PHP 联系表)
- git - 如何在 git 中将 2 个拉取请求从一个分支合并到另一个分支
- reactjs - 异步笑话测试中的 TSServer 误报
- mysql - mysql 在列中插入值:int DEFAULT 0 导致错误
- android - 如何在应用程序中有条件地选择模块依赖或源代码?