入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2017-04-07 投稿人: 浏览次数:3428


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

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。