表名为score,用一条sql,查询出该表每门课都大于80分的学生姓名;
name kecheng fenshu jiguan
张三 语文 75 天津
张三 数学 80 河北
李四 语文 90 上海
李四 数学 70 天津
王五 语文 90 唐山
王五 数学 90 石家庄
①select `name` from score group BY `name` having min(fenshu) >80;
②先查询课程小于等于80的学生姓名 在筛掉这些人
SELECT DISTINCT `name` FROM score
WHERE `name` NOT in
( select distinct `name` from score where fenshu<=80)
当表中加上籍贯字段,查询出天津人数和非天津人数
①union 将两个查询结果组合到一起
SELECT COUNT(*) as 天津人数 FROM score WHERE jiguan = '天津'
union all
SELECT COUNT(*) as 非天津人数 FROM score WHERE jiguan != '天津';
②case when then (多重if)
select
count(case when jiguan='天津' then jiguan end) as 天津人数,
count(case when jiguan!='天津' then jiguan end) as 非天津人数
from score;