SQL查询解决方案?

时间:2016-03-20 22:48:28

标签: sql

如果我必须从一组表格中找到数据,例如所有电影的长度,以及名字" Roderigo",这是我会怎么做的?

SELECT SUM(m.length) AS "Films with Roderigo"
FROM movie m
INNER JOIN movie_actor x
ON (m.MOVIE_ID = x.MOVIE_ID)
INNER JOIN actor a
ON (x.ACTOR_ID = a.ACTOR_ID)
WHERE a.actor_lname = 'Roderigo';

1 个答案:

答案 0 :(得分:1)

不,这不是你一般的做法。它可能在某些情况下有用。但是如果你有两个名字叫罗德里戈的演员在同一部电影中播放,那你可以两次看电影。

SELECT SUM(m.length) AS total_length
FROM movie m
WHERE EXISTS (
  SELECT *
  FROM movie_actor x INNER JOIN
    actor a ON x.ACTOR_ID = a.ACTOR_ID
  WHERE a.actor_fname = 'Roderigo' AND m.MOVIE_ID = x.MOVIE_ID
)

SELECT SUM(m.length) AS total_length
FROM movie m
WHERE m.MOVIE_ID IN (
  SELECT x.MOVIE_ID
  FROM movie_actor x INNER JOIN
    actor a ON x.ACTOR_ID = a.ACTOR_ID
  WHERE a.actor_fname = 'Roderigo'
)