-- 6、表中有ABC三列,当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列 create table abc( a int, b int, c int ); insert into abc values(22,24,23); select * from abc; create database gk; use gk; create table gaokao ( kh int(8) , km char(3), cj int ); use gk; insert into gaokao values(2006001,'语文',119), (2006001,'数学',108), (2006002, '物理',142), (2006001, '化学',136), (2006001, '物理',127), (2006002, '数学',149), (2006002, '英语' ,110), (2006002, '语文' ,105), (2006001, '英语' ,98), (2006002, '化学' ,129); select * from gaokao ; -- 输出高考理科综合总分在300以上且所有科目成绩在600以上的考生的准考证号,并依据总成绩从高到低排序。 select kh,cj from gaokao where km='数学'; select kh,cj from gaokao where km='化学'; select kh,cj from gaokao where km='物理'; ##第一步 查询理科综合成绩大于300的学生 select a.kh,a.cj 数学,b.cj 化学,c.cj 物理,sum(a.cj+b.cj+c.cj) 综合成绩 from (select kh,cj from gaokao where km='数学') a left join (select kh,cj from gaokao where km='化学') b on a.kh=b.kh left join (select kh,cj from gaokao where km='物理') c on a.kh=c.kh group by kh having 综合成绩>300; ##第二步 子查询并且总分在600分以上的同学 select kh from( select a.kh,a.cj 数学,b.cj 化学,c.cj 物理,sum(a.cj+b.cj+c.cj) 综合成绩 from (select kh,cj from gaokao where km='数学') a left join (select kh,cj from gaokao where km='化学') b on a.kh=b.kh left join (select kh,cj from gaokao where km='物理') c on a.kh=c.kh group by kh having 综合成绩>300) z where z.kh in (select kh from gaokao group by kh having sum(cj)>600);