mysql case decode_@Marked-今天的一个SQL题-case语句和decode函数

数据库表:

select * from rec order by rst,game_time;

ID GAME_TIME      RST

------ -------------- ----

2 01-1月 -11     F

6 01-1月 -11     F

3 02-1月 -11     F

9 02-1月 -11     F

7 03-1月 -11     F

1 01-1月 -11     W

4 01-1月 -11     W

8 01-1月 -11     W

5 02-1月 -11     W

要求结果:

比赛日期       结果   结果统计

-------------- ---- ----------

02-1月 -11     失败          2

03-1月 -11     失败          1

02-1月 -11     胜利          1

01-1月 -11     失败          2

01-1月 -11     胜利          3

写出SQL1:decode函数

select

game_time as 比赛日期,

decode(rst,'F','失败','W','胜利','无结果') as 结果,

count(rst) as 结果统计

from rec

group by game_time,rst;

SQL2:case语句:

select

game_time as 比赛日期,

(case rst when 'W' then '胜利'

when 'F' then '失败'

else '无结果'

end)结果,

count(rst) as 结果统计

from rec

group by game_time,rst;

记录下:

1.  decode函数用法:

decode(表达式1,条件1,结果1,[条件2,结果2][default]);

2.  case 语句:

case 表达式

when 表达式1 then ....

when 表达式2 then ....

else  ......

end 表达式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值