服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|

服务器之家 - 数据库 - Mysql - mysql case when group by 实例详解

mysql case when group by 实例详解

2020-08-23 17:32MYSQL教程网 Mysql

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

mysql 中类似php switch case 的语句。

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

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

?
1
2
3
select sum(redpackmoney) as stota,ischeck
from t_redpack
group by isCheck

mysql case when group by 实例详解

使用case when :

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

mysql case when group by 实例详解

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

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

延伸 · 阅读

精彩推荐