题解 | 统计各等级会员用户下订单总额

统计各等级会员用户下订单总额

https://www.nowcoder.com/practice/48dd35a3dd8c4e1494db36b097a03300

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

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务