您的位置:首页 > 数据库 > MySQL

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

2017-04-07 10:28 615 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: