SQLZOO练习题(2)

SQLZOO练习题(2)

sqlzoo上较难的一些sql查询练习题及其解题思路。

在这里插入图片描述

题目
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') asc, subject, winner

思路
subject IN (‘Chemistry’,‘Physics’)可以作为值来使用,满足时为1,不满足为0。

要将物理和化学排在最后,可以利用这个值来做文章。在order by的首项填入subject in (‘physics’, ‘chemistry’) asc,表示按奖项进行排序,不是物理和化学的为0,是的为1,asc升序排序为0,1,这样就将物理和化学奖排到最后了。

希望这篇文章对你有所帮助。

猜你喜欢

转载自blog.csdn.net/weixin_44054605/article/details/88803203