sqlzoo.net刷题

时间:2023-12-24 09:27:55

只发后面提升题目的题解,前面的太简单,写下来也没有意义

12.查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EUGENE O'NEILL

select * from nobel where winner ='EUGENE O\'NEILL'

题目推荐用两个‘符号来转义

个人还是推荐linux系统常用的转义符号\,这个基本上是类unix的传统

13.列出爵士的獲獎者、年份、獎頁(爵士的名字以Sir開始)。先顯示最新獲獎者,然後同年再按名稱順序排列。

这个对显示出来的字段有顺序要求,不能直接 select * from 否则会出错

select winner,yr,subject from nobel where winner like 'Sir%' order by yr desc ,winner asc

14.

The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.

Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

这个题目没有中文,翻译的大概意思是 按照获奖的科学领域跟获奖者的名字来排序,但是 化学和物理要被排在最后

SELECT winner, subject FROM nobel where yr=1984 ORDER BY subject IN ('Physics','Chemistry'),subject asc,winner asc

这里分析一下,以后也用得上,关键在order by subject IN ('Physics','Chemistry') ,subject asc,winner asc

后两个比较容易理解 字段名加上asc表示按正常排序,难点在 subject in (xxx)这个表达式,

排除后两个表达式,这是一个分组排序,

subject in(xxx)为0的分成一组 排序

subject in(xxx)为1的分成一组 排序

得到结果连接起来就是新的排序表