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

本文主要介绍了MySQL中的COALESCE()和IFNULL()函数在处理NULL值时的区别,通过实际SQL查询示例展示如何在订单表中使用这两个函数,以及在其他场景如客户信息查询中的应用。
MySQL中COALESCE()和IFNULL()的区别-优快云博客
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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值