Case具有兩種格式。簡單Case函數和Case搜索函數
遇到 xxx情況 就 xxx 遇不到就 xxx結束
case when …… then…… else……end
--簡單Case函數
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函數
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END
select *,
count(CASE WHEN consultType = 1 then 1 end) as count1,
count(CASE WHEN consultType = 0 then 1 end ) as count2,
count(CASE WHEN consultType = 2 then 1 end) as count3
from jcb_users AS ju INNER JOIN jcb_user_consult_record AS jucr where ju.loginUserId=130
我的例子