题解 | #牛客的课程订单分析(三)#
牛客的课程订单分析(三)
http://www.nowcoder.com/practice/4ae8cff2505f4d7cb68fb0ec7cf80c57
这一道题收获挺多。有很多细节性的问题需要注意。
比如,第一,group by()后面的having()中,一般只写聚合函数的过滤规则,其他所有的过滤条件都在where语句中写出;
第二,再复杂一点的检索语句无非就是不断的套娃,一定要注意细节。
-- 先找出符合条件的id
-- 然后根据Id及相关过滤条件查找订单信息;
select *
from order_info
where user_id in (
select user_id
from (select user_id, count(product_name) as cnt
from order_info
where product_name in ('C++','Python','Java')
and status = 'completed'
and date > '2025-10-15'
group by user_id
) as a
where cnt >= 2)
and status = 'completed'
and date >= '2025-10-15'
and product_name in ('C++','Python','Java')
order by id;
MySQL试题答案解析 文章被收录于专栏
MySQL在线编程重点试题解析

