mysql中case⽤法1.
SELECT * ,
CASE
WHEN CLS = '0' THEN
'特约商户'
WHEN CLS = '1' THEN
'城市应⽤'
WHEN CLS = '2' THEN
'服务⽹点'
END typeDescriTion
FROM branch_type
WHERE DEL_FLAG = '0'
总结 :会基于 对应的 匹配 输出不同的值
2.
select id ,(case  when sex= ''  then 'bbbbb'
when sex is null then 'aaaaa'
else sex end  ) as sex FROM aa;
3 .
SELECT
SUM(CASE WHEN CLS = '0' THEN 1 ELSE 0 END) AS zerocount1 , SUM(CASE WHEN CLS = '1' THEN 1 ELSE 0 END) AS zerocount2 , SUM(CASE WHEN CLS = '2' THEN 1 ELSE 0 END) AS zerocount3 FROM branch_type
这个是 类似与分组
4.
SELECT
CASE TYPE
WHEN '0' THEN
'幼⼉园'
WHEN '1' THEN
'⼀年级'
WHEN '2' THEN
'⼆年级'
WHEN '3' THEN
'三年级'
WHEN '4' THEN
'四年级'
WHEN '5' THEN
'五年级'
WHEN '6' THEN
'六年级'
WHEN '7' THEN
'七年级'
WHEN '8' THEN
'⼋年级'
WHEN '9' THEN
'九年级'
END AS TYPE,
CASE state
WHEN '0' THEN
'待审核'
WHEN '1' THEN
'审核通过'
WHEN '2' THEN
'未通过审核'
END AS STATE FROM test_when_case;
5.
SELECT s.SHOP_NAME,
CASE
WHEN c.COUPON_VALUE = '-1' THEN
(CONCAT(c.BEGIN_COUPON_VALUE,'~',c.END_COUPON_VALUE))
ELSE CONCAT(c.COUPON_VALUE) END,
CASE
WHEN c.COUPON_CLASS = 'share' THEN
'⽀付分享券'
WHEN c.COUPON_CLASS = 'follow' THEN
'新粉关注券'
WHEN c.COUPON_CLASS = 'default' THEN
'商城内部券'
END AS couponClass, CONCAT(c.COUPON_COUNT), CONCAT(c.SEND_COUNT),
CASE
WHEN c.USED_COUNT IS NULL THEN
'0'
ELSE CONCAT(c.USED_COUNT) END, CONCAT(c.BEGIN_TIME), CONCAT(c.END_TIME),
CASE
WHEN STATUS = 0 THEN
'未停⽤'
WHEN STATUS = 1 THEN
'已停⽤' END, CONCAT(MIN_AMOUNT)
FROM shop s
INNER JOIN coupon c
ON s.SHOP_ID = c.SHOP_ID
6.
SELECT c.*
FROM programme c, live l
WHERE (CASE
WHEN c.update_time >= c.end THEN
c.update_time
d END)<![CDATA[<]]> #{now}
AND (CASE
WHEN c.update_time >= c.end THEN
c.update_time
d END)<![CDATA[>]]> DATE_FORMAT(SUBDATE(STR_TO_DATE(#{now},'%Y%m%d%H%i'),INTERVAL20 MINUTE),'%Y%m%d%H%i')        AND c.status = 0
AND c.channel_id = l.id
AND (l.palyback ISNULL
OR l.palyback = '是')
7.
SELECT * ,
CASE WHEN STATUS ='2' AND RESULT ='1' THEN '成功'
WHEN STATUS ='2' AND RESULT ='0' THEN '失败'
WHEN STATUS ='0' AND RESULT IS NULL THEN '未开始'
WHEN STATUS ='1' AND RESULT IS NULL THEN '进⾏中'
WHEN STATUS ='-1' AND RESULT IS NULL THEN '已撤销'
ELSE '有问题的'
END 结果
FROM publish_task
SELECT COUNT(1),STATUS,RESULT FROM publish_task WHERE 1=1 GROUP BY STATUS,RESULT
8.
select c.*
from programme c, live l
where (case when c.update_time >= c.end then c.update_time d END)<![CDATA[<]]> #{now}
and (case when c.update_time >= c.end then c.update_time d END)<![CDATA[>]]>
DATE_FORMAT(SUBDATE(STR_TO_DATE(#{now},’%Y%m%d%H%i’),INTERVAL20 MINUTE),’%Y%m%d%H%i’)
and c.status = 0
and c.channel_id = l.id
and (l.palyback isnull or l.palyback = ‘是’)
【注】”programme “、”live “为MySQL中的两个表,#{now}为参数
isnull的用法SELECT
from programme ,live -------- programme 、live表, 定义programme为c,live为l
case -------------如果
when c.update_time >=c.end then c.update_time ------------- c.update_time >= c.end,则返回值c.update_time
d -------------其他的返回c.end
end -------------结束
<![CDATA[>]]> ---------------在 XML 元素中,"<" 和 "&" 是⾮法的。"<" 会产⽣错误,因为解析器会把该字符解释为新元素的开始。"&" 也会产⽣错误,因为解析器会把该字符解释为字符实体的开始。某些⽂本,⽐如 JavaScript 代码,包含⼤量 "<" 或 "&" 字符。为了避免错误,可以将脚本代码定义为 CDATA。CDATA 部分中的所有内容都会被解析器忽略。
DATE_FORMAT( ) ---------⽤于以不同的格式显⽰⽇期/时间数据。
DATE_FORMAT(date,format)
date 参数是合法的⽇期。format 规定⽇期/时间的输出格式。
SUBDATE( ) ---------从⽇期减去指定的时间间隔。与DATE_SUB()同义
DATE_SUB(date,INTERVAL expr type)
date 参数是合法的⽇期表达式。expr 参数是您希望添加的时间间隔。
STR_TO_DATE( ) -------字符串转换为⽇期。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。