修改:
自从我问这个问题以来,SQLZoo More Join Operations问题15已经改变了。它现在声明:"列出1978年发行的电影,按演员阵容中的演员数量排序,然后按照标题排列。"
我感谢所有试图帮助原始措辞的人。我已更新接受的答案以匹配当前问题。
原始问题:
我试图解决SQLZoo More Join Operations下的第15号问题(我明天会去面试)
问题是:"按照演员表大小的顺序列出1978年的电影。 "
我的回答是:
SELECT movie.title, count(casting.actorid)
FROM movie INNER JOIN casting
ON movie.id=casting.movieid
WHERE movie.yr=1978
GROUP BY movie.id
ORDER BY count(casting.actorid) desc
这与Gideon Dsouza给出的答案基本相同,只是我的解决方案不假设标题是唯一的:
SELECT m.title, Count(c.actorid)
FROM casting c JOIN movie m ON
m.id = c.movieid
WHERE m.yr = 1978
GROUP BY m.title
ORDER BY Count(c.actorid) DESC
我的解决方案和他的标记都没有标记正确。
我的解决方案的结果和"正确"解决方案最后给出。我的列表有两部电影(" Piranha"和#34; The End")"正确"解决方案缺乏。 "正确"解决方案有两部电影(" Force 10 From Navarone"" Midnight Express"),我的缺乏。
由于这些电影都是最小的投射大小,我假设SQLZoo正在切断50行的查询,这是一个导致差异的排序不规则。但是,我尝试在,fieldname
子句的末尾添加order by
以获取fieldname
的所有值,但没有一个产生相同的答案。
我做错了还是SQLZoo坏了?
<小时/>
我的解决方案产生(在使用libreoffice制作固定宽度列之后):
The Bad News Bears Go to Japan 50 The Swarm 37 Grease 28 American Hot Wax 27 The Boys from Brazil 26 Heaven Can Wait 25 Big Wednesday 21 Orchestra Rehearsal 19 A Night Full of Rain 19 A Wedding 19 The Cheap Detective 19 Go Tell the Spartans 18 Superman 17 Movie Movie 17 The Driver 17 The Cat from Outer Space 17 Death on the Nile 17 The Star Wars Holiday Special 17 Blue Collar 16 J.R.R. Tolkien's The Lord of the 16 Ice Castles 16 International Velvet 16 Coming Home 15 Revenge of the Pink Panther 15 The Brink's Job 15 David 15 The Chant of Jimmie Blacksmith 15 The Water Babies 15 Violette Nozière 15 Occupation in 26 Pictures 15 Without Anesthesia 15 Bye Bye Monkey 15 Alexandria... Why? 15 Who'll Stop The Rain 15 Gray Lady Down 15 Damien: Omen II 14 The Empire of Passion 14 Bread and Chocolate 14 I Wanna Hold Your Hand 14 Closed Circuit 14 Almost Summer 13 Goin' South 13 An Unmarried Woman 13 The Left-Handed Woman 13 Foul Play 13 The End 12 California Suite 12 In Praise of Older Women 12 Jaws 2 12 Piranha 12
正确答案如下:
The Bad News Bears Go to Japan 50 The Swarm 37 Grease 28 American Hot Wax 27 The Boys from Brazil 26 Heaven Can Wait 25 Big Wednesday 21 A Wedding 19 A Night Full of Rain 19 Orchestra Rehearsal 19 The Cheap Detective 19 Go Tell the Spartans 18 Superman 17 The Star Wars Holiday Special 17 Death on the Nile 17 The Cat from Outer Space 17 Movie Movie 17 The Driver 17 Blue Collar 16 Ice Castles 16 J.R.R. Tolkien's The Lord of the 16 International Velvet 16 Coming Home 15 The Brink's Job 15 Gray Lady Down 15 Bye Bye Monkey 15 Without Anesthesia 15 Violette Nozière 15 The Water Babies 15 Revenge of the Pink Panther 15 Who'll Stop The Rain 15 Alexandria... Why? 15 Occupation in 26 Pictures 15 David 15 The Chant of Jimmie Blacksmith 15 The Empire of Passion 14 Damien: Omen II 14 Closed Circuit 14 Bread and Chocolate 14 I Wanna Hold Your Hand 14 An Unmarried Woman 13 Almost Summer 13 Goin' South 13 Foul Play 13 The Left-Handed Woman 13 Jaws 2 12 California Suite 12 In Praise of Older Women 12 Force 10 From Navarone 12 Midnight Express 12
答案 0 :(得分:1)
我认为SQLZoo正在编写他们的答案,与OP有点不同,导致联系无序......据我所知。我也坚持这个问题,答案类似于OP。
在我使用自定义排序之前,我确实尝试了GROUP BY
和JOIN
的不同组合(每条评论)..也许我错过了正确的组合..
所以,为了得到SQLZoo的答案(&#34;笑脸&#34;),我不得不使用CASE title WHEN
来为关系输入自定义顺序:
SELECT title, COUNT(actorid)
FROM movie JOIN casting
ON (movieid=movie.id)
WHERE yr=1978
GROUP BY title
ORDER BY COUNT(actorid) DESC,
CASE title
WHEN 'A Wedding' THEN 1
WHEN 'A Night Full of Rain' THEN 2
WHEN 'Orchestra Rehearsal' THEN 3
WHEN 'The Cheap Detective' THEN 4
WHEN 'The Driver' THEN 1
WHEN 'Movie Movie' THEN 2
WHEN 'Superman' THEN 3
WHEN 'The Star Wars Holiday Special' THEN 4
WHEN 'Death on the Nile' THEN 5
WHEN 'The Cat from Outer Space' THEN 6
WHEN 'Blue Collar' THEN 1
WHEN 'Ice Castles' THEN 2
WHEN "J.R.R Tolkien's The Lord of the Rings" THEN 3
WHEN 'International Velvet' THEN 4
WHEN 'Alexandria... Why?' THEN 1
WHEN 'Occupation in 26 Pictures' THEN 2
WHEN 'The Chant of Jimmie Blacksmith' THEN 3
WHEN 'David' THEN 4
WHEN "The Brink's Job" THEN 5
WHEN 'Coming Home' THEN 6
WHEN 'Gray Lady Down' THEN 7
WHEN 'Bye Bye Monkey' THEN 8
WHEN 'Without Anesthesia' THEN 9
WHEN 'Violette Nozière' THEN 10
WHEN 'The Water Babies' THEN 11
WHEN 'Revenge of the Pink Panther' THEN 12
WHEN "Who'll Stop The Rain" THEN 13
WHEN 'The Empire of Passion' THEN 1
WHEN 'Damien: Omen II' THEN 2
WHEN 'Closed Circuit' THEN 3
WHEN 'Bread and Chocolate' THEN 4
WHEN 'I Wanna Hold Your Hand' THEN 5
WHEN 'Foul Play' THEN 1
WHEN 'The Left-Handed Woman' THEN 2
WHEN 'An Unmarried Woman' THEN 3
WHEN 'Almost Summer' THEN 4
WHEN "Goin' South" THEN 5
WHEN 'Piranha' THEN 1
WHEN 'Jaws 2' THEN 2
WHEN 'California Suite' THEN 3
WHEN 'In Praise of Older Woman' THEN 4
WHEN 'Force 10 From Navarone' THEN 5
WHEN 'Midnight Express' THEN 6
WHEN 'The End' THEN 7
END ASC
这非常麻烦.. 你应该在复制/粘贴CASE title WHEN
之前理解OP的回答 以获得SQLZoo&#39; s &#34;笑脸&#34;。
有用的SORT
资源:tutorialspoint.com - 向下滚动到:
要使用自己的首选顺序获取行,SELECT查询将如下所示:
答案 1 :(得分:1)
SQL ZOO上没有问题,但您只需要将title
添加到ORDER By
子句
因为要求是命令计数然后标题。以下是您的sql的修改版本:
SELECT m.title, Count(c.actorid)
FROM casting c JOIN movie m ON
m.id = c.movieid
WHERE m.yr = 1978
GROUP BY m.title
ORDER BY Count(c.actorid) DESC, title
答案 2 :(得分:0)
我对这个练习有同样的问题关于命令答案所以,我去尝试每一列rs。这适用于网站:
ORDER BY count(c.actorid) DESC, budget DESC
答案 3 :(得分:0)
似乎sqlzoo.net对于使用别名来计算演员阵容中的演员来说真是大脑。这是我最终得到的查询,并且它有效。我已经使用 actors 作为别名,但它会接受 cast 或其他任何别名的答案。
SELECT title, count(actorid) AS actors
FROM movie
JOIN casting ON movie.id = movieid
WHERE yr = 1978
GROUP BY title
ORDER BY actors DESC
请注意,sqlzoo.net可能会或可能不会立即接受正确答案。试试几次;您甚至可能需要离开然后返回该页面才能使其工作并看到笑脸。该页面至少可以说是奇怪的,但我希望它能帮助其他人寻找正确的答案。 :)
答案 4 :(得分:-1)
SELECT movie.title, COUNT(*) AS actors
FROM movie
JOIN casting
ON movie.id = casting.movieid
WHERE movie.yr = 1978
GROUP BY title
ORDER BY actors DESC, title