1、數據準備ui
有以下數據,學生id,課程,分數spa
1,yuwen,43
1,shuxue,55
2,yuwen,77
2,shuxue,88
3,yuwen,98
3,shuxue,65code
2、需求分析orm
一、建立表blog
create table requirement( sid int, course string, score int ) row format delimited fields terminated by ',';
二、上傳數據rem
load data local inpath '/usr/mydir/data/requirement.txt' into table requirement;
三、驗證數據是否正確string
select * from requirement;
四、查詢數據it
方法一:table
select sid, max(math) as math, max(chinese) as chinese from ( select *, case course when 'shuxue' then score else 0 end as math, case course when 'yuwen' then score else 0 end as chinese from requirement) t group by sid having math > chinese;
方法二:form
SELECT a.sid,a.score math,b.score chinese FROM (select sid,course,score FROM requirement where course = 'shuxue') a left join (select sid,course,score FROM requirement where course = 'yuwen') b on a.sid = b.sid where a.score >= b.score;
!注意:使用方法二進行鏈接查詢,運行時所消耗的時間較多!
3、結果