SQLZOO: SELECT from Nobel Tutorial/zh

nobel
yrsubjectwinner
1960 Chemistry Willard F. Libby
1960 Literature Saint-John Perse
1960 Medicine Sir Frank Macfarlane Burnet
1960 Medicine Peter Madawar
...
nobel(yr, subject, winner)

yr: 年份
subject: 獎項
winner: 得獎者

#备注用于记录做SQLZOO中的Nobel相关的题目,用于记录

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

SELECT * FROM nobel
WHERE winner='Eugene O'Neill'

#这里需要注意的是在获奖名中有一个单引号,需要在单引号前加来转义。

13.

騎士列隊 Knights in order

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

SELECT winner,yr,subject FROM nobel
WHERE winner LIKE 'Sir%'
ORDER BY yr desc,winner asc

#需要注意的是在排序过程中要使用ORDER BY

#desc——降序排序

#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 ('Chemistry','Physics') asc, subject asc,winner asc

#这里需要注意的是使用

subject IN ('Chemistry','Physics') asc 实现分组排序,符合要求的是逻辑值1,反之为0,
利用asc使('Chemistry','Physics')位于列尾;
subject asc是将逻辑列表中的0和1中各自列表中的名字进行排序;

winner asc 是将winner的名字进行升序排序
原文地址:https://www.cnblogs.com/gegemu/p/13625418.html