题解 | #牛客的课程订单分析(三)#
牛客的课程订单分析(三)
https://www.nowcoder.com/practice/4ae8cff2505f4d7cb68fb0ec7cf80c57
题目要求
查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的订单信息,并且按照order_info的id升序排序,以上例子查询结果如下:
v1版错误代码
思路:符合条件的用户 下过的所有订单
审题错误了,因此无法通过
select o.* from order_info o left join ( select distinct user_id, count(id)over(partition by user_id) as order_count from order_info where date > '2025-10-15' and status = 'completed' and product_name in ('C++','Java','Python') )n on o.user_id = n.user_id where n.order_count >= 2 order by o.id asc
v2版正确代码
在这些条件范围内 下过的订单信息(date,status,课程,用户是(在2025-10-15以后xxx且下单两次以上的))
select * from order_info where date > '2025-10-15' and status = 'completed' and product_name in ('C++','Java','Python') and user_id in ( select distinct o.user_id from order_info o left join ( select distinct user_id, count(id)over(partition by user_id) as order_count from order_info where date > '2025-10-15' and status = 'completed' and product_name in ('C++','Java','Python') )n on o.user_id = n.user_id where n.order_count >= 2 )