SQL加入SQLzoo

时间:2014-10-23 04:45:20

标签: mysql sql sql-server sqlite

在MySQL的更多连接操作下,SQLzoo存在问题。 问题是

列出1978年发布的影片,以演员阵容中的演员人数排序。

给出的表格如下,ord的值为0或1,1对于星形角色。

movie | actor | casting
id       id      movieid
yr       name    actorid
title             ord
budget
gross
director

我写了我的SQL查询如下,SQLzoo没有告诉我我有正确的答案,但我相信我的查询是正确的。有人可以验证这一点,并可能告诉我如何在没有子查询的情况下这样做吗?谢谢!

SELECT DISTINCT 
  movie.title, f.num_actors
FROM
  (SELECT
     casting.movieid,COUNT(casting.actorid) AS num_actors
   FROM
     casting
   GROUP BY
     casting.movieid ) f
JOIN
  movie ON f.movieid = movie.id
WHERE
  movie.yr = 1978
ORDER BY
  2 DESC

7 个答案:

答案 0 :(得分:3)

str="aaa.bbb.1.ccc.[mmm]."

答案 1 :(得分:2)

SELECT title,count(actorid) FROM movie join casting on movie.id=movieid join
actor on actor.id=actorid WHERE yr='1978' GROUP by title ORDER by
count(actorid) desc, title

嗨,大家好,我知道我有点迟了但还是会试着帮助:)这就是我做的。部分说明是按演员阵容中的演员数量,然后按标题排序。解释代码的最后部分。

答案 2 :(得分:1)

select title, count(actorid)
from movie 
join casting on movie.id = movieid
where yr = 1978
group by title
order by count(actorid) desc, title asc

答案 3 :(得分:0)

看起来SQLzoo数据已被损坏'现在,它不接受正确的查询。下面将给出两个看起来不正确的条目。

选择*来自 施放movieid = 20136 和actorid = 179 按命令排序

答案 4 :(得分:0)

SQLZoo没有问题,但您必须将SQL的最后一部分从2 DESC更改为f.num_actors DESC, title。以下是您的sql的修改版本:

SELECT DISTINCT 
  movie.title, f.num_actors
 FROM
  (SELECT
     casting.movieid,COUNT(casting.actorid) AS num_actors
    FROM
    casting
    GROUP BY
    casting.movieid ) f
 JOIN movie ON f.movieid = movie.id
WHERE movie.yr = 1978
ORDER BY f.num_actors DESC, title

不使用内部选择的其他解决方法是:

SELECT title, count(casting.actorid)
 FROM movie 
           JOIN casting ON casting.movieid = movie.id
 WHERE yr=1978
 GROUP BY title
 ORDER BY count(casting.actorid) DESC, title

答案 5 :(得分:0)

答案相对简单

select title, count(actorid) from movie join casting on movie.id=movieid join actor on actor.id=actorid where yr=1978 group by title order by count(actorid) desc, title

答案 6 :(得分:-1)

您可以先进行加入,然后进行分组:

SELECT movie.*
FROM movie
JOIN casting ON movie.id = casting.movieid
WHERE movie.yr = 1978
GROUP BY movie.id
ORDER BY COUNT(*) DESC