日期:2014-05-18  浏览次数:20742 次

怎么在select语句中加判断条件
现在有一个表

table 
age_level child_num child_pay child_time adult_num adult_pay adult_time older_num older_pay older_time
1 ....

根据age_level来抓取要的 num pay time三个值

当age_level = 1 的时候返回 child_num child_pay child_time 的值作为 num pay time
当age_level = 2 的时候返回 adult_num adult_pay adult_time 的值作为 num pay time
当age_level = 3 的时候返回 older_num older_pay older_time 的值作为 num pay time

这样的sql语句怎么写?

------解决方案--------------------
SQL code
select age_level,
(case when age_level = 1 then Chile_num when age_level=2 then adult_num when age_level=3 then older_num end)as num,
(case when age_level = 1 then Chile_pay when age_level=2 then adult_pay when age_level=3 then older_pay end)as pay,
(case when age_level = 1 then Chile_time when age_level=2 then adult_time when age_level=3 then older_time end)as time
from table

------解决方案--------------------
SQL code
decode(age_level,1,child_num ,2,adult_num, older_num)num ,
decode(age_level,1,child_pay ,2,adult_pay, older_pay)pay ,
decode(age_level,1,child_time,2,adult_time, older_time)time,