SELECT
case
when sex='1' then '男'
when sex='2' then '女'
else 0
end
from test
上面语句意思:test表中如果sex=’1’,则返回值’男’如果sex=’2’,则返回值’女’ 否则返回‘0’结束
再举个例子 统计年龄指定范围内的人数
大于20小于30
大于30小于50
select sum(case when age > 20 and age < 30
then 1 else 0 end) as "num",
sum(case when age > 30 and age < 50
then 1 else 0end) as "num",
form test;