sql系列之001_{case~~when~~ then}
来源:互联网 发布:Mac网络游戏 编辑:程序博客网 时间:2024/06/05 23:52
1、等值判断,相当于switch case
WHEN value1 THEN returnvalue1
WHEN value2 THEN returnvalue2
WHEN value3 THEN returnvalue3
ELSE defaultreturnvalue
END
例如:
case level
when 1 then '大叔'
when 2 then '二叔'
when 3 then '小叔'
end
相当于Switch,只能等值判断。
select username,叔级别=(case levelwhen 1 then '大叔'when 2 then '二叔'when 3 then '小叔'end)from userinfo其结果为:~~~
2、区间判断
CASE
WHEN condition1 THEN returnvalue1
WHEN condition 2 THEN returnvalue2
WHEN condition 3 THEN returnvalue3
ELSE defaultreturnvalue
END
相当于if…else if…else….(可以进行区间判断)
注意:then后面返回的数据类型要一致, returnvalue1、 returnvalue2、 returnvalue3的数据类型必须一致。
select username,年纪=( case when age between 28 and 30 then '大叔' when age between 25 and 27 then '大青年' when age between 20 and 24 then '骚年' when age < 20 then '小屁孩' else 'baby' end)from userinfo2
3、实际案例扯淡
1、一般论坛中的用户等级
论坛中用户的等
case BBSlevel
when 1 then '骨灰'
when 2 then '大虾'
when 3 then '菜鸟'
end
2、订单表中,统计每个销售员的总销售金额,列出销售员名、总销售金额、称号(>10W金牌,>5W银牌,>1W铜牌,否则普通)
3、电商用户 根据用户消费总额 予以不同的等级
4、游戏行业 玩家等级
单号 金额
RMB1 10
RMB2 20
RMB3 -30
RMB4 -10
将上面的表输出为如下的格式:
单号 收入 支出
RMB1 10 0
RMB2 20 0
RMB3 0 30
RMB4 0 10
select number,(case when amount > 0 then amountelse 0end) 收入,(case when amount < 0 then abs(amount)else 0end ) 支出from test
- sql系列之001_{case~~when~~ then}
- SQL之 case when then
- SQL之CASE,WHEN,THEN
- SQL之CASE,WHEN,THEN
- sql --case when then
- sql case when then
- sql case when then
- SQL之case when then用法详解
- SQL之case when then用法
- SQL之case when then用法
- SQL之case when then用法
- SQL之case when then用法
- SQL之case when then用法详解
- SQL之case when then用法
- SQL之case when then用法
- SQL之case when then用法
- sql中的case、when、then
- sql--case when then 示例
- Unix/Linux 缩写风格
- GNU GDB Debugger
- JavaScript 对象简介
- 百钱买白鸡,为神魔没法运行呢?求高人指点!!!
- Nuclear.Coffee.Recover.Keys.v6.0.2.67.Multilingual-PH
- sql系列之001_{case~~when~~ then}
- Hdu 1005
- 抗日系列之台儿庄大捷
- 代码之谜(四)- 浮点数(从惊讶到思考)
- Project Euler problem 45
- Jquery框架下Ajax与PHP数据交换
- Web前端之网页导航----搜导航狐邮箱(float)
- 无根的根:无名师的 Unix 心传
- unistd --> getopt && getopt_long