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
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中查询字段为null或者不为null
- mysql 查询字段为null或者非null
- mysql查询字段为null的方法
- MySQL 字段值为 null,但查询时用 is null 没用的问题
- 查询字段值为null、0、空字符串的记录
- MySQL查询时给字段赋默认值
- MySQL ifnull函数判断字段值为null时使用默认值
- mysql查询字段为null 返回0
- mysql中查询字段为null或者不为null
- mysql 对查询为null的值赋默认值
- Access 中值为空字符串字段与值为null字段的查询问题
- mysql中查询字段为null或者不为null
- mysql中查询字段为null或者不为null
- MySQL ifnull函数判断字段值为null时使用默认值
- mysql中查询字段为null或者不为null
- MySQL如何正确查询字段为NULL的字段
- mysql查询表字段默认值
- mysql 中查询一个字段是否为null的sql
- mysql 数据库字段为datetime类型时默认值为:0000-00-00 00:00:00 查询时异常 java.sql.SQLException:Value '0000-00-00' can not be represented as java.sq
- mysql中查询字段为null或者不为null