mysql查询字段为空字符串时给默认值 (2、为null时给一默认值)


1、

case when post_ask_pay.price='' then 10 else post_ask_pay.price end as price

SELECT distinct post_ask_pay.user_id as ask_user_id,post_ask_pay.ask_id,post_ask.expert_id,FROM_UNIXTIME(post_ask_pay.create_time, '%Y-%m-%d %H:%i:%S') as pay_ask_time,concat(post_ask_pay.ask_id,post_ask_pay.create_time) order_id,case when post_ask_pay.price='' then 10 else post_ask_pay.price end as price from post_ask_pay
                         LEFT JOIN post_ask on post_ask_pay.ask_id = post_ask.ask_id
                         WHERE post_ask_pay.answer_count = -1 and post_ask.expert_id is not null and post_ask_pay.create_time >= UNIX_TIMESTAMP('2017-03-11')

2、 coalesce函数

    表示可以返回参数中的第一个非空表达式,当你有N个参数时选取第一个非空值(从左到右)。 

        实例一: 

              select coalesce(null,"carrot","apple") 

          返回结果:carrot

你可能感兴趣的:(mysql)