题解 | #返回产品名称和每一项产品的总订单数#

返回产品名称和每一项产品的总订单数

https://www.nowcoder.com/practice/1c64fd9048364a58a8ffa541720359a4

/*select prod_name,
       orders 
   from (
select prod_name ,
       if(a.orders is not null,a.orders,0) orders 
  from Products P 
  left join 
  (
    select prod_id ,
           count(order_num) orders 
      from OrderItems OI 
      group by OI.prod_id 
  ) a  on P.prod_id = a.prod_id 

  union 
select prod_name ,
       if(a.orders is not null,a.orders,0) orders
  from Products P 
  right join 
  (
    select prod_id ,
           count(order_num) orders 
      from OrderItems OI 
      group by OI.prod_id 
  ) a  on P.prod_id = a.prod_id 
) b order by prod_name ;   
**/  

select prod_name ,
       count(order_num) orders 
       from (
select prod_name,
       order_num 
  from Products P 
  left join OrderItems OI using(prod_id) 
  union 
select prod_name,
       order_num 
  from Products P 
  right join OrderItems OI using(prod_id)  
) a  
group by prod_name 
order by prod_name  ;   




全部评论

相关推荐

10-13 16:58
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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