Re: [SQL ] 資料選取問題

看板Database作者 (rock)時間8年前 (2016/03/11 23:18), 編輯推噓2(208)
留言10則, 3人參與, 最新討論串2/2 (看更多)
※ 引述《littlepigred (小豬鴻)》之銘言: : 資料庫名稱:MySQL : 資料庫版本: : 內容/問題描述: : 今天去面試遇到一題題目如下: : name subject score : ──────────── : aaa math 90 : aaa chem 75 : aaa eng 70 : bbb chem 85 : bbb math 95 : bbb eng 90 : ccc eng 65 : ccc chem 80 : ccc math 75 : 請用SQL語法選出至少2科分數高於85且沒有任何一科低於75的人 : 小弟新手 麻煩各為求解QQ select name from( select name ,count(case when score>84 then 1 else null end) as [Over85] ,count(case when score<75 then 1 else null end) as [Below75] from scoretb group by name) tmp where [Over85]>=2 and [Below75]=0 -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.194.8 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1457709503.A.735.html

03/12 00:42, , 1F
我是用MSSQL,MySQL您要試試看
03/12 00:42, 1F

03/12 01:31, , 2F
select name from tab where score >= 85 and exists
03/12 01:31, 2F

03/12 01:32, , 3F
(select name from tab where score <=75)
03/12 01:32, 3F

03/12 01:32, , 4F
group by name having count(name)>=2;
03/12 01:32, 4F

03/12 01:32, , 5F
我是用oracle
03/12 01:32, 5F

03/12 01:33, , 6F
<=75那邊有錯,應該是<75
03/12 01:33, 6F

03/12 23:51, , 7F
我是原po 謝謝各位的解答 考試只說用SQL語法 沒特
03/12 23:51, 7F

03/12 23:51, , 8F
別設限
03/12 23:51, 8F

03/12 23:52, , 9F
不過我用MYSQL 好像不能用exists執行 我再研究 不過
03/12 23:52, 9F

03/12 23:52, , 10F
概念我了解了 謝謝各位!
03/12 23:52, 10F
文章代碼(AID): #1Muk6_Sr (Database)
文章代碼(AID): #1Muk6_Sr (Database)