MySQL case when 使用

程序里错误码和字典对应不上,要做二次加工,回顾了一下case when的使用,直接在SQL里处理一下

case when的两种写法

简单判断:

要判断值的列写在case后面,when后面写值,相等的情况执行then后的语句,可以写多个when判断语句,都不满足返回else后的值,要以end结尾,end后面可以给该列指定列名。

CASE [col_name]
WHEN [value1] THEN [result1]
WHEN [value1] THEN [result2]
....
ELSE [default]
END [new_col_name]

表达式判断:

case开头,when后面写表达式,表达式为真则执行then后的语句,可以写多个when判断表达式,都不满足的情况下返回else后的值,以end结尾,end后可以为该列指定列名。

CASE
WHEN [expr] THEN [result1]
WHEN [expr] THEN [result2]
....
ELSE [default]
END [new_col_name]

case when可以写在select后,可以写在where语句中,也可以联合表的时候写在联合的on语句中

下面是对错误码进行二次加工的例子

select a.khh,a.warning_type ,b.error_type,b.error_name,count(*)
from (
select khh,warning_type, error_code from t_client_response_info where log_date>=‘10838‘ and log_date<‘10853‘
) a
LEFT JOIN  t_error_code b
on
a.warning_type = b.alarm_type and
(case
    when SUBSTR(a.error_code, 1, 4) in (‘0002‘,‘0005‘,‘0006‘,‘0007‘,‘0008‘,‘0009‘)  then CONCAT(SUBSTR(a.error_code, 1, 4),‘0000‘)
    when SUBSTR(a.error_code, 1, 6) in (‘000C01‘,‘000C02‘)  then CONCAT(SUBSTR(a.error_code, 1, 6),‘00‘)
    when SUBSTR(a.error_code, 1, 3) =‘066‘ and SUBSTR(a.error_code, 1, 6) != ‘06600‘ then CONCAT(SUBSTR(a.error_code, 1, 5),‘000‘)
    when SUBSTR(a.error_code, 1, 2) =‘06‘ and SUBSTR(a.error_code, 1, 3) != ‘066‘ then CONCAT(SUBSTR(a.error_code, 1, 2),‘00‘,SUBSTR(a.error_code, 5, 4))
    else a.error_code
end) = b.error_code
group by
a.khh,a.warning_type,b.error_type,b.error_name;

原文地址:https://www.cnblogs.com/zhaoshizi/p/11552286.html

时间: 2024-10-14 19:04:17

MySQL case when 使用的相关文章

MySQL case when 用法 详细举例,可创建数据演示

首先我们创建数据库表: CREATE TABLE `t_demo` ( `id` int(32) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(2) DEFAULT NULL, `num` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 插入数据: INSERT INTO `t_demo` VALUES ('1', '张三',

mysql case when用户示例

看到一些朋友在使用bind-dlz时写的mysql语句一知半解,索性自己动手写几个示例实验一下,加深印象,废话不多说,直解上料: 1)原始记录 mysql> select id,name from test;+------+------+| id   | name |+------+------+|    1 | lili ||   10 | yaya ||   30 | cici |+------+------+ 2)语法CASE input_expression    WHEN when_e

MySQL CASE表达式

一.MySQL为您提供了两种形式的CASE表达式. 1.CASE表达式的第一种形式: CASE value WHEN compare_value_1 THEN result_1 WHEN compare_value_2 THEN result_2 WHEN compare_value_3 THEN result_3 WHEN compare_value_4 THEN result_4 WHEN compare_value_5 THEN result_5 WHEN compare_value_6

mysql case

1.table CREATE TABLE `lee`(`id` INT(10) NOT NULL auto_increment,`name` varchar(20) DEFAULT null,`birthday` datetime DEFAULT null,PRIMARY KEY(`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8; 2.data INSERT INTO lee(name,birthday) VALUES('sam','1990-01-01');I

mysql case when

select a.*, case when a.age = 1 then '一岁了' when a.age = 2 then '二岁了' when a.age >= 3 and a.age <= 8 then ( case when a.age = 3 then '三岁了' when a.age = 4 then '四岁了' when a.age = 5 then '五岁了' when a.age = 6 then '六岁了' when a.age = 7 then '七岁了' when a.

mysql case then使用

表的创建CREATE TABLE `lee` (`id` int(10) NOT NULL AUTO_INCREMENT, `name` char(20) DEFAULT NULL, `birthday` datetime DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 数据插入:insert into lee(name,birthday) values ('sam','1990-01-01');inser

MySQL case when条件分支判断

举例说明 SELECT CASE a.AFTER_QUALITY WHEN '0' THEN '过免维期' WHEN '1' THEN '未过免维期' ELSE '错误数据' END AS AFTER_QUALITY FROM t_maintain_log a 数据库表内容: 运行结果:

mysql case when &amp; concat &amp; SUBSTRING_INDEX &amp; not &amp; having 使用的小case

1. 代码 SELECT a.id, a.activity_name, ( CASE WHEN a.activity_end_time > now() THEN '参与中' ELSE ( CASE WHEN ( a.activity_doubt <> '*' AND a.activity_doubt < c.doubt ) OR ( a.activity_praise <> '*' AND a.activity_praise > c.praise * 100 )

Mysql CASE WHEN 用法

select sum(1) as col_0_0_, sum(case vciinfo.useable when -1 then 1 else 0 end) as col_1_0_, sum(case when vciinfo.activatetime is null then 0 else 1 end) as col_2_0_ from vci_info vciinfo 这是一个用case when 实现统计表中不同状态数据数量的sql语句 这里要注意的是 判断字段是否为空用 case whe