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

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

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

方法1

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

方法2

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(user_id) >= 2
order by user_id;

这道题本来就是一个方法2就可以解决的,但是我忘了聚合函数的使用条件,不得不找了一个麻烦的方法,来一点一点的剥离剖析。

聚合函数的使用:首先要有分组GROUP,其次可以在SELECT语句中使用(但是只能包含聚合函数和分组列),也可以在HAVING语句中使用。

MySQL试题答案解析 文章被收录于专栏

MySQL在线编程重点试题解析

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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