mysql中的case when then,replace的用法。

SELECT ys.person_id,a.name,CASE WHEN ys.gender='1' THEN '男' ELSE '女' END,YEAR(CURDATE())-YEAR(ys.birth_date) AS age,ys.id_card_code,ys.birth_date,CASE ys.edu WHEN 1 THEN '文盲及半文盲' WHEN 2 THEN '小學' WHEN 3 THEN '初中' WHEN 4 THEN '高中/技校/中專' WHEN 5 THEN '大學專科及以上' WHEN 6 THEN '不詳' END,a.address,CASE ys.job 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 '待業' WHEN 0 THEN '空' END,ys.disease,m.bp_left_h,m.bp_left_l,m.bmi,m.waist_line,CASE m.exercise_frequency WHEN '1' THEN '每天' WHEN '2' THEN '每週一次以上' WHEN '3' THEN '偶爾' WHEN '4' THEN '不鍛鍊' END,m.each_exercise_time,m.adhere_exercise_time,REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(m.adhere_exercise_style,'1','散步'),'2','慢跑'),'3','長跑'),'4','單槓'),'5','游泳'),'6','跳繩'),'7','舞蹈'),'8','球類運動'),'9','氣功'),'a','太極'),'b','體操'),'c','其它'),m.exercise_other,REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(m.diet,'1','葷素均衡'),'2','葷食爲主'),'3','素食爲主'),'4','嗜鹽'),'5','嗜油'),'6','嗜糖') AS diet,CASE m.smoking_status WHEN '1' THEN '從不吸菸' WHEN '2' THEN '已戒菸' WHEN '3' THEN '吸菸' END,m.smoking_day,m.age_started_smoking,m.age_quit_smoking,case m.drinking_frequency when '1' then '從不' when '2' then '偶爾' when '3' then '經常' when '4' then '每天' end,m.drinking_day,case m.alcoholics when '1' then '未戒酒' when '2' then '已戒酒' end,m.alcoholics_age,m.age_started_drinking,case m.drunk when '1' then '是' when '2' then '否' end,REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(m.drinking_category,'1','白酒'),'2','啤酒'),'3','紅酒'),'4','黃酒 '),'5','其他'),m.drinking_category_r,m.fbg,m.hemoglobin,m.leucocyte,m.platelet,m.routine_other,m.proteinuria,m.urine_sugar,m.urine_ketone,m.urine_occult,m.urinalysis_other,m.microalbuminuria,CASE m.stool_occult WHEN  '1' THEN '陰性' when '2' then '陽性' END,m.sgpt,m.sgot,m.albumin,m.total_bilirubin,m.combined_bilirubin,m.serum_creatinine,m.bun,m.spc,m.sodium_concentration,m.tc,m.tg,m.ldlc,m.hdlc,m.glycohemoglobin,CASE m.hepatitis WHEN '1' THEN '陰性' when '2' then '陽性' END,m.hepatitis_remark,CASE m.ecg  WHEN '1' THEN '正常' when '2' then '異常' END,m.ecg_remark,CASE m.chest_xray WHEN '1' THEN '正常' when '2' then '異常' END,m.chest_xray_remark,CASE m.bray WHEN '1' THEN '正常' when '2' then '異常' END,m.bray_remark,CASE m.cervical_smear WHEN '1' THEN '正常' when '2' then '異常' END,m.cervical_smear_remark,m.auxiliary_other,case when length(m.health_evaluation_r)=0 then '體檢無異常' else m.health_evaluation_r end FROM ys_base_info AS ys LEFT JOIN archives AS a ON ys.person_id = a.id LEFT JOIN medical m ON a.id = m.person_id WHERE (m.counter=(SELECT MAX(counter) FROM medical WHERE person_id = a.id) and ys.isReported='1' AND (m.fbg <7.0 OR m.fbg is null) AND ys.disease NOT LIKE '%糖尿病%') AND ((ys.gender='1' AND m.waist_line >=90) OR (ys.gender='2' AND m.waist_line >=85) OR (m.bmi>=24) OR (ys.disease LIKE '%冠心病%' OR ys.disease LIKE '%腦卒中%' OR ys.disease LIKE '%高血壓%') OR (YEAR(CURDATE())-YEAR(ys.birth_date)>=40) OR (m.exercise_frequency<>'1' or m.exercise_frequency is null) OR (m.exercise_frequency='1' AND m.each_exercise_time<=30) OR (m.tc>=5.18 or m.tg >= 2.26)) and ys.isReported='1' and (YEAR(CURDATE())-YEAR(ys.birth_date)>18) and DATE_FORMAT(m.create_date,'%Y')='2013'
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章