1. 程式人生 > 其它 >Mysql將查詢出的數值轉換為中文顯示case..when..then

Mysql將查詢出的數值轉換為中文顯示case..when..then

我們經常需要在資料庫匯出檔案,可是匯出某些欄位時不是中文含義其它同事分不清。可以通過case..when..then根據一一對應的關係將值轉成中文,再進行匯出方便大家查閱。

1、正常sql未處理之前查詢結果是數值,直接看無法分辨數值是什麼含義(此sql查詢的是禪道 啟用+已處理 狀態的bug詳情,轉發給同事查閱)

select zb.id 'BugID',zp.name '產品',zbu.name '版本',zb.title 'Bug標題', zb.severity 'Bug嚴重級別',zu.realname '指派開發',zd.name '開發組別',zb.status '狀態',zus.realname '
建立人',
zb.openedDate'建立時間' from zt_bug zb left join zt_product zp on zp.id=zb.product left join zt_user zus on zb.openedBy=zus.account left join zt_user zu on zb.assignedTo=zu.account left join zt_build zbu on zbu.id=zb.openedBuild left join zt_dept zd on zu.dept=zd.id where zb.deleted='0' and zb.status in
('resolved','active')

 2、sql將要轉義的欄位處理為中文顯示:case 欄位 when "原本查詢出的結果" then "想轉義成什麼結果" end "欄位重新命名顯示"

select zb.id 'BugID',zp.name '產品',zbu.name '版本',zb.title 'Bug標題',case zb.severity when '1' then '致命' when '2' then '嚴重' when '3' then '一般' when '4' then '建議' end 'Bug嚴重級別',
zu.realname '指派開發',zd.name '
開發組別',case zb.status when 'closed' then '已關閉' when 'resolved' then '已解決' when 'active' then '啟用' end '狀態',
zus.realname '建立人',zb.openedDate'建立時間' from zt_bug zb left join zt_product zp on zp.id=zb.product left join zt_user zus on zb.openedBy=zus.account left join zt_user zu on zb.assignedTo=zu.account left join zt_build zbu on zbu.id=zb.openedBuild left join zt_dept zd on zu.dept=zd.id where zb.deleted='0' and zb.status in ('resolved','active')