题解 | #牛客的课程订单分析(三)#

牛客的课程订单分析(三)

http://www.nowcoder.com/practice/4ae8cff2505f4d7cb68fb0ec7cf80c57

第一种解法
1.找出满足题目要求的user_id;
2.按照题目要求 select 出全部字段;
3.uesr_id in(1 中 select 出的 user_id);

select * 
from order_info
where date>'2025-10-15'
and product_name in('C++','Java','Python')
and status ='completed'
and user_id in (select user_id
                from order_info
                where date>'2025-10-15'
                and status='completed'
                and product_name in('C++','Java','Python')
                group by user_id
                having count(*)>=2)

第二种解法
1.先满足题目要求 select 出 user_id,然后用 with as 临时存储为一个视图;
2.和1一样的条件,只不过现在不用分组聚合,user_id 条件为 select * from a;
with as 在后期需要反复用到临时视图时,可大大降低代码量,以及提高可读性,本题的作用有限

with  a  as
(select user_id
                from order_info
                where date>'2025-10-15'
                and status='completed'
                and product_name in('C++','Java','Python')
                group by user_id
                having count(*)>=2)
select * 
from order_info
where date>'2025-10-15'
and product_name in('C++','Java','Python')
and status ='completed'
and user_id in (select * from a)
order by id asc
全部评论

相关推荐

身边有人上海、深圳 6、7k 都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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