请告诉我如何在mysql 2005中使用带有CASE条件的if语句

时间:2017-07-14 09:39:59

标签: mysql if-statement case

Table :-Person
Column:-day_phone, alt_phone    ,Home_phone

条件: - 如果day_phone为null,则为alt_phone
如果两者中都没有数据则复制Home_phone

2 个答案:

答案 0 :(得分:0)

用例和AND运算符

  select Person
  , case when day_phone is null then alt_phone
        when day_phone is null and lt_phone is null then Home_phone
    end
  from my_table 

答案 1 :(得分:0)

您可以使用将返回第一个非空值的COALESCE函数:

select coalesce(day_phone, alt_phone, Home_phone)
from person