第七章 6-ifnull and coalesce函数|the ifnull and coalesce functions--Mosh my SQL

MySQL中COALESCE()和IFNULL()的区别-CSDN博客
1,查找没有shipper_id的记录,并把标题改为shipper

use sql_store;
select
    order_id,
    ifnull(shipper_id,'not assigned') as shipper
from orders

2,用coalesce函数,如果shipper_id是空的,返回comments那列的值,如果comments也是空的,就显示not assigned

use sql_store;
select
    order_id,
    coalesce(shipper_id,comments,'not assigned') as shipper
from orders

对比原表,没有问题

3,在2的基础上,加上一列    ifnull(shipper_id,'...'),shipper_id null 的时候显示...

use sql_store;
select
    order_id,
    ifnull(shipper_id,'...'),
    coalesce(shipper_id,comments,'not assigned') as shipper
from orders

4,练习:写一段查询

答案:这里用ifnull也是可以的

use sql_store;
select
    concat(first_name, ' ',last_name)as customer,
    coalesce(phone,'unknown') as phone
from customers

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值