select product_id, product_name, sum(quantity) as total_quantity, round(average_rating, 2) as average_rating from sales_underline left join products_underline using (product_id) join ( select product_id, avg(rating) as average_rating from reviews_underline group by product_id having average_rating <= 4 ) as t using (product_id) group by product_id, product_name order by average_rating ASC, product_id ASC;
看了很多其他同学的解法,我真的想说,无脑联成宽表丝毫不考虑笛卡尔积还有滥用临时表,在实际大数据量情况下代码表现通常都很糟糕,不要以为系统给你过了就是对的,希望大家多多精进,少写慢sql