select u.vip	,sum(COALESCE(o.order_price,0)) as order_total

from 
uservip_tb u 
left join 
order_tb o
on u.user_id = o.user_id

group by u.vip
order by order_total desc

第一个是又确定了一遍left join的逻辑,会生成类似

1 订单号1

1 订单号2

1 订单号3

1 订单号4

这样的表

使用left join的时候可能会产生null的值,所以在计算的时候要讲null的值进行转换

就是coalesce函数的功能

类似如果用户手机号是空值,就返回第二个值

COALESCE(user_phone, '未预留联系方式') AS contact_info

或者用户的手机号没有,邮箱有,就会返回邮箱,两个都没有返回第三个字符串

COALESCE(user_phone, user_email, '未预留联系方式') AS contact_info