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

被折叠的 条评论
为什么被折叠?



