case .. when ...then .. else .. end
条件判断函数
举例1:
select name ,
sum( case when result = 'win' then 1 else 0 end ) as 'win',
sum(case when result = 'lose' then 1 else 0 end) as 'lose',
from score
group by name
从胜负记录表中,查询出每个人胜场个数和败场个数。
举例2:
select name,subject,score,
case
when score>80 and score < 100 then '优秀'
else
case when score > 60 then '合格' else '不合格' end
end
from test_score
case内部在包含一个case,输出成绩结果的判断,80-100为优秀,60-80合格,60以下的不合格
网友评论