简单思路,先选出符合条件的user_id然后选符合条件的订单
牛客的课程订单分析(六)
https://www.nowcoder.com/practice/c5736983c322483e9f269dd23bdf2f6f
简单思路,先选出符合条件的user_id,然后选出符合条件的订单信息,不太好的就是判断条件要用两遍
第一步,求和
select user_id, count(1) ct from order_info where date > '2025-10-15' && status = 'completed' && (product_name = 'C++' || product_name = 'Java' || product_name = 'Python') group by user_id
第二步,选出符合条件的user_id
select user_id from ( select user_id, count(1) ct from order_info where date > '2025-10-15' && status = 'completed' && (product_name = 'C++' || product_name = 'Java' || product_name = 'Python') group by user_id )t1 where ct >= 2
第三步,选出相应信息,要left join 因为左表中有右表没有的连接条件
select o1.id,is_group_buy,name client_name from order_info o1 left join client c1 on o1.client_id = c1.id where user_id in( select user_id from ( select user_id, count(1) ct from order_info where date > '2025-10-15' && status = 'completed' && (product_name = 'C++' || product_name = 'Java' || product_name = 'Python') group by user_id )t1 where ct >= 2 ) && date > '2025-10-15' && status = 'completed' && (product_name = 'C++' || product_name = 'Java' || product_name = 'Python'