首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(二)
  • 热度指数:80251 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

有很多同学在牛客购买课程来学习,购买会产生订单存到数据库里

有一个订单信息表(order_info),简况如下:

id user_id product_name
status
client_id
date
1 557336
C++
no_completed
1 2025-10-10
2 230173543
Python
completed
2 2025-10-12
3 57 JS
completed
3 2025-10-23
4 57 C++
completed
3 2025-10-23
5 557336
Java
completed
1 2025-10-23
6 57 Java
completed
1 2025-10-24
7 557336
C++
completed
1 2025-10-25

1行表示user_id557336的用户在2025-10-10的时候使用了client_id1的客户端下了C++课程的订单,但是状态为没有购买成功。

2行表示user_id230173543的用户在2025-10-12的时候使用了client_id2的客户端下了Python课程的订单,状态为购买成功。

。。。

最后1行表示user_id557336的用户在2025-10-25的时候使用了client_id1的客户端下了C++课程的订单,状态为购买成功。

请你写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的user_id,并且按照user_id升序排序,以上例子查询结果如下:

user_id
57
557336
解析:

id为46的订单满足以上条件,输出对应的user_id57;

id为57的订单满足以上条件,输出对应的user_id557336;

按照user_id升序排序。

示例1

输入

drop table if exists order_info;
CREATE TABLE order_info (
id int(4) NOT NULL,
user_id int(11) NOT NULL,
product_name varchar(256) NOT NULL,
status varchar(32) NOT NULL,
client_id int(4) NOT NULL,
date date NOT NULL,
PRIMARY KEY (id));

INSERT INTO order_info VALUES
(1,557336,'C++','no_completed',1,'2025-10-10'),
(2,230173543,'Python','completed',2,'2025-10-12'),
(3,57,'JS','completed',3,'2025-10-23'),
(4,57,'C++','completed',3,'2025-10-23'),
(5,557336,'Java','completed',1,'2025-10-23'),
(6,57,'Java','completed',1,'2025-10-24'),
(7,557336,'C++','completed',1,'2025-10-25');

输出

57
557336
select u.user_id as 'user_id'
from(
select user_id,
       product_name,
       status,
       date,
count()over(partition by user_id) as 'count_user'
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in ('C++','Java','Python')
and count_user >= 2)as u 
order by u.user_id asc;

用了窗口函数,可为啥有点怪怪的呢
发表于 2022-08-16 23:07:54 回复(0)
#思路  简单题目,在题目一的基础上进行分组,然后数量大于1
select user_id
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in ('C++','Python','Java')
group by user_id having count(*)>1
order by user_id


发表于 2022-04-19 15:34:50 回复(0)
select distinct(a.user_id) from (
    select *,
    count(product_name)over(partition by user_id) as c 
    from order_info
    where date >'2025-10-15'
    and status='completed'
    and (product_name='C++'
        &nbs***bsp;product_name='Java'
        &nbs***bsp;product_name='Python')
)a
where a.c>=2
order by a.user_id 

发表于 2021-08-18 18:26:10 回复(0)
主要还是对于分组函数使用

select user_id
from order_info
where date > '2025-10-15' and status = 'completed'
and product_name in ('C++', 'Python', 'Java')
group by user_id
having count(*) > 1
order by user_id


发表于 2021-04-06 21:49:17 回复(1)
这道题就是在第一题的基础上加一个group by分组,再将聚合函数count(user_id)放入到having关键字之后进行筛选,要注意的就是聚合函数只能用having筛选,非聚合函数更推荐用where筛选,因为where效率更高
select user_id
from order_info
where date > '2025-10-15'
and product_name in ('C++','Java','Python')
and status = 'completed'
group by user_id
having count(user_id) > 1
order by user_id
发表于 2021-03-01 09:17:04 回复(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

发表于 2021-07-04 13:00:25 回复(0)
#方法一:group by +having 还能在select后没有跟聚合函数的时候这样当筛选条件用,前提是group by的字段和select的字段相同
select distinct user_id
from order_info
where  product_name in ('C++','Java','Python')
and status='completed'
and date >'2025-10-15'
group by user_id
having count(id)>=2
order by user_id;
方法二:订单数>=2的用户利用子查询做筛选
(在SQL中验证了查询结果是对的,但是不知道为何系统通不过)
select distinct user_id
from order_info
where user_id in 
(select user_id from (select user_id
                        ,count(id)
                     from order_info
                     group by user_id
                     having count(id)>=2 )t1)
and product_name in ('C++','Java','Python')
and status='completed'
and date >'2025-10-15'
order by user_id;



编辑于 2021-03-01 11:41:37 回复(8)
select 
    user_id 
from 
    order_info 
where 
    status='completed' 
and 
    product_name in ('C++','Java','Python')
and 
    date > '2025-10-15'
group by 
    user_id 
having 
    count(status)>=2
order by 
    user_id

筛选条件正确即可。

发表于 2021-02-28 10:53:18 回复(0)
简单题
# 请你写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的user_id,并且按照user_id升序排序

select user_id from
(
    select user_id,count(*)  as cnt
    from order_info
    where date>date("2025-10-15")
    and product_name  in ("C++","Java","Python")
    and status="completed"
    group by user_id
    having cnt>=2
)tmp1
order by 1



发表于 2023-03-24 08:27:43 回复(0)
select distinct(user_id)
from order_info
where  user_id in (
    select user_id
    from order_info
    where status = 'completed' and product_name in ('C++','Java','Python'and date > '2025-10-15' 
    group by user_id
    having count(user_id) >= 2
)
order by user_id
发表于 2022-11-04 14:40:23 回复(0)
select distinct
    a.user_id
from
    (
        select
            t.user_id,
            count(t.user_id) user_id_cnt
        from
            order_info t
        where
            t.date > '2025-10-15'
            and t.status = 'completed'
            and t.product_name in ("C++", "Java", "Python")
        group by
            t.user_id
    ) a
where
    a.user_id_cnt >= 2
order by
    a.user_id

发表于 2025-02-08 10:56:32 回复(0)
select
    user_id
from
    order_info
where
    date > '2025-10-15' and product_name in ('C++','Java','Python') and status = 'completed'
group by
    user_id
having
    count(product_name) >= 2
order by
    user_id

发表于 2025-01-09 11:16:35 回复(0)
select user_id
from order_info
join (select user_id,count(*) sums from order_info
where status = 'completed' and date > '2025-10-15' and product_name in ('C++','Java','Python')
group by user_id) sum using(user_id)
where sums >= 2
group by user_id
order by user_id
忘记了having
发表于 2025-01-07 13:39:01 回复(0)
with t1 as (
select user_id,count(id) as aa
from order_info
where status="completed" and date>="2025-10-15"and product_name in("C++","Java","Python")
group by user_id
)
select user_id
from t1
where aa>=2
order by user_id#注意where的多重筛选条件
发表于 2024-12-10 13:51:22 回复(0)
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 

发表于 2024-11-27 16:06:22 回复(0)
select user_id 
from order_info 
where product_name in('C++','Python','Java') 
and date>'2025-10-15'
group by user_id 
having sum(case when status = 'completed' then 1 else 0 end)>1 
order by user_id

发表于 2024-11-07 21:51:29 回复(0)
SELECT
    user_id
FROM order_info
WHERE date > '2025-10-15' AND product_name IN ('C++', 'Java', 'Python')
GROUP BY user_id
HAVING SUM(
            CASE
                WHEN status = 'completed' THEN 1
                ELSE 0
            END
) >= 2
ORDER BY user_id
发表于 2024-10-26 15:44:19 回复(0)
select distinct user_id
from
(select *,
(count(*) over (partition by user_id)) as cnt
from order_info
where date >= '2025-10-15'
and product_name in ('C++','Java','Python')
and status = 'completed'
)a
where cnt >= 2;
发表于 2024-09-29 05:51:39 回复(0)
子查询为什么这条通过不了。
select user_id from (select user_id,
count(case when product_name in ('C++','Python','Java') then 1 else 0 end) sum
from order_info where date>'2025-10-15'
and status='completed'
group by user_id) as oi where oi.sum>1  order by user_id
发表于 2024-09-27 17:09:53 回复(0)
select user_id
from (
    select *
    from order_info
    where date > '2025-10-15' and status = 'completed' and product_name in ('C++', 'Java', 'Python')
) as t
group by user_id
having count(*) >= 2
order by user_id

发表于 2024-09-23 09:56:05 回复(0)