http://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial
1、selectyr,subject,winner
from nobel
where yr = 1950
2.、select winner
from nobel
where yr='1962' and subject='Literature'
說明:where語句中對表示條件的需要用單引號。
3、select yr,subject
from nobel
where winner='Albert Einstein'
4、select winner
from nobel
where subject= 'Peace'and yr>='2000'
5、selectyr,subject,winner
from nobel
where subject='Literature'and yr between1980 and 1989
6、selectyr,subject,winner
from nobel
where winner in ('TheodoreRoosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama')
7、select winner
from nobel
where winner like 'John%'
8、selectyr,subject,winner
from nobel
where subject='Physics' and yr=1980
or (subject='Chemistry'and yr='1984')
或者
Select * from nobel
Where subject='Physics' and yr=1980
or (subject='Chemistry'and yr='1984')
9、select * fromnobel
where subject not in ('Chemistry','Medicine')and yr='1980'
10、select * fromnobel
where subject= 'Medicine' and yr<'1910'
or subject= 'Literature' and yr>='2004'
11、select * fromnobel
where winner='PETER GRüNBERG'
說明:元音字母“ü”輸入方法為“alt+0220”即可。
12、select * fromnobel
where winner='EUGENE O''NEILL'
說明:條件語句中的“EUGENE O'NEILL”因為含有單引號,需與sql語句中的單引號區(qū)分,所以在本身名稱單詞中已經(jīng)包含的單引號使用兩個單引號,即為'EUGENE O''NEILL'
13、selectwinner,yr,subject from nobel
where winner like 'Sir%'
order by yr desc,winner desc
或者select winner,yr,subject from nobel
where winner like 'Sir%'
order by yr desc,winner
說明:原題中要求先顯示最新獲獎?wù)撸偻臧凑彰Q順序排序,則表示時間順序排序為降序,即用“desc”,
14、原題:Show the 1984winners and subject ordered by subject and winner name; but list Chemistry andPhysics last.
Select winner,subject from nobel
WHERE YR='1984'
order by subjectin('Chemistry','Physics'),subject,WINNER
或者
SELECT winner, subject, subject IN('Physics','Chemistry')
?FROM nobel
?WHERE yr=1984
?ORDER BY subject,winner
說明:
使用ORDER
BY配合IN語句
原題要求按照項目subject和獲獎?wù)咝彰鹷inner排序,同時要求獲獎項目為Physics和Chemistry的排在最后。
Subject in()配合order
by 語句的意思是subject in(’Physics’,’Chemistry’)進(jìn)行判斷,如果屬于項目Physics或?qū)儆陧椖緾hemistry,則為真返回數(shù)值“1”,如果不屬于,則為假返回數(shù)值“0”,按照默認(rèn)的asc升序排序,則判斷為真返回數(shù)值為“1”的會排在最后,所以項目為Physics和Chemistry的自然就會排在最后。同時order by逗號后面的內(nèi)容“,subject,winner”意為 將剩下不屬于Physics和Chemistry的數(shù)據(jù)按照默認(rèn)升序排序asc;姓名也按照默認(rèn)升序排序asc。
參考資料:
MySQL
ORDER BY 排序 IF 及IN
https://blog.csdn.net/bestallen/article/details/53726192
????@?3?;#?6?