题目描述:sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的user_id,并且按照user_id升序排序。
select user_id from order_info where datediff(date,"2025-10-15")>0 and status ="completed" and product_name in("C++","Java","Python") group by user_id having count(id)>=2 order by user_id
按题意一步一步写条件即可。
注意where子句常见错误之一:
where子句中不能使用聚合函数,聚合函数可以在select,having,order by之后出现。
where指定分组之前数据行的条件,having子句用来指定分组之后条件。
所以下面这条语句是错误的:
select user_id from oder_info where datediff(date,"2025-10-15")>0 and count(id)>=2 and status ="completed" and product_name in ("C++","Java","Python") group by user_id order by user_id