记录一次Mysql 的Case 的使用

记录一次Mysql 的Case 的使用

case 命令的模式:

CASE case_value
	WHEN when_value THEN
		statement_list
	ELSE
		statement_list
END CASE;

一个case 的使用:

SELECT  
      CASE category 
      when  'student'  then 0
      when  'company'  then 1
	  ELSE '344'
	  END as 'level'
from
( 
	SELECT ur.category
	from user_role urr
	LEFT JOIN role ur
	on urr.role_id = ur.id
)   as tt;

多个case 的使用:

SELECT *,
  (CASE uc.flag
   when uc.flag=1 then '充值'
   when uc.flag=2 then '提現'
   when uc.flag=3 then '投资'
  END),
 (CASE 
   when uc.status=1 then '正在申请'
   when uc.status=2 then '待确认'
   when uc.status=3 then '已确认'
  END),
  (CASE uc.account
   WHEN 1 THEN '数字货币'
    when 1 then '美元'
END), 
  uc.balance as '余额'
from user_cash uc
LEFT JOIN user u
on uc.user_id = u.id;

猜你喜欢

转载自blog.csdn.net/wind1_rain/article/details/108411987
今日推荐