1. 程式人生 > >mysql查詢欄位為空字串時給預設值 (2、為null時給一預設值)

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