题解 | #返回顾客名称和相关订单号以及每个订单的总价#

返回顾客名称和相关订单号以及每个订单的总价

https://www.nowcoder.com/practice/4dda66e385c443d8a11570a70807d250

select
    a.cust_name,
    b.order_num,
    sum(c.quantity * c.item_price) as OrderTotal
from
    Customers a
    left join Orders b on a.cust_id = b.cust_id
    join OrderItems c on b.order_num = c.order_num
group by
    a.cust_id,
    a.cust_name,
    b.order_num
order by
    a.cust_name,
    b.order_num

不太理解这个题通过率才35....

a,b,表左连接,再内连接c, 左连接是为了可能存在有的顾客没有下单的情况(不管有没有这种可能,生产环境都要考虑这些问题,你也可以认为我考虑多了)

这里group by 了三个字段,加上了cust_id,是为了避免用户cust_name出现重复的情况,造成问题(你也可以认为我考虑多了)

全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
10-16 15:48
算法工程师
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

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