电脑疯子技术论坛|电脑极客社区

 找回密码
 注册

QQ登录

只需一步,快速开始

[编程和数据库] mysql case when group by 实例详解

[复制链接]
zhaorong 发表于 2018-1-9 14:59:59 | 显示全部楼层 |阅读模式

这篇文章主要介绍了mysql 中类似php switch case 的语句,需要的朋友可以参考下

mysql 中类似php switch case 的语句。

select xx字段,  
case 字段
        when 条件1 then 值1  
        when 条件2 then 值2
        else 其他值 END 别名
from 表名;


下面是一个分组求和示例:

  1. select sum(redpackmoney) as stota,ischeck
  2. from t_redpack
  3. group by isCheck
复制代码


20180102221800.png

使用case when :

  1. select sum(redpackmoney) as stota,
  2. (CASE isCheck WHEN '1' THEN 'checktrue' WHEN '0' THEN 'uncheck'
  3. WHEN '-1' THEN 'checkfalse' ELSE 'unknow' END) as checkinfo
  4. from t_redpack
  5. group by isCheck
复制代码


20180102221801.png

checkinfo中 -1, 0, 1 替换为 checkfalse, uncheck, checktrue(CASE WHEN进行字符串替换处理)

以上就是mysql中case when语句的简单使用示例介绍。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|VIP|电脑疯子技术论坛 ( Computer madman team )

GMT+8, 2025-2-2 21:53

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表