Mysql case when else simple to use

Today, I suddenly received an order from my superiors asking for statistics on the success rate of the interface. Calculate the proportion of success and failure in a certain period of time. Since the superiors are not programmers, they have to count the forms they can recognize.

Before modification:

SELECT
	`status`,
	count(*) AS '次数' 
FROM
	sys_task_log 
WHERE
	task_id = '56' 
	AND DATE_FORMAT( createTime, '%y%m%d' ) >= DATE_FORMAT( '2019-12-15', '%y%m%d' ) 
	AND DATE_FORMAT( createTime, '%y%m%d' ) <= DATE_FORMAT( '2019-12-16', '%y%m%d' ) 
GROUP BY
	`status`;

In order to display the meaning of 0 and 1 in the result set, I use the case statement of mysql to judge the value of each record. If the record is 1, it means success; otherwise, it fails. The SQL is as follows:

SELECT
CASE
		`status` 
		WHEN 1 THEN '成功'
		ELSE '失败' 
	END '状态',
	count(*) AS '次数' 
FROM
	sys_task_log 
WHERE
	task_id = '56' 
	AND DATE_FORMAT( createTime, '%y%m%d' ) >= DATE_FORMAT( '2019-12-15', '%y%m%d' ) 
	AND DATE_FORMAT( createTime, '%y%m%d' ) <= DATE_FORMAT( '2019-12-16', '%y%m%d' ) 
GROUP BY
	`status`

 

 

Guess you like

Origin blog.csdn.net/Lixuanshengchao/article/details/103564860