首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(二)
  • 热度指数:84567 时间限制: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 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;
发表于 2025-04-06 22:59:29 回复(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)
子查询为什么这条通过不了。
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 DISTINCT
    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 IN ( "C++", "Java", "Python" ) 
    ) AS a 
WHERE
    c > 1 
ORDER BY
发表于 2024-09-05 15:51:33 回复(0)

考验group by + having 的用法吧

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(id) >= 2
order by user_id asc
发表于 2024-08-13 17:50:12 回复(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(*)>=2
order by user_id;

发表于 2024-02-07 14:47:35 回复(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 ) > 1
order by user_id

发表于 2023-07-21 14:07:56 回复(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(user_id) >= 2
order by 
    user_id

发表于 2023-05-25 16:52:32 回复(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 (select *,count(product_name)over(partition by user_id) rk
from order_info
where date>'2025-10-15' and status='completed' and product_name in('C++','Java','Python')) a
where a.rk>=2
order by user_id

发表于 2023-03-19 12:12:37 回复(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(*) >= 2
order by user_id;

发表于 2022-12-31 11:47:59 回复(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 user_id
from order_info
where status = 'completed' and date >'2025-10-15'
and product_name in ('C++','Java','Python')
group by user_id
having count(status)>=2
order by user_id;

发表于 2022-10-31 14:39:27 回复(0)
select user_id
from order_info
where datediff(date,'2025-10-15') > 0
and product_name in ('C++','Java','Python')
and status = 'completed'
group by user_id
having count(product_name) >=2
order by user_id
#where 子句中不能使用聚合,所以count()跟在group by后面的条件having中
#btw,聚合也可以在select及order by 后面

发表于 2022-10-27 08:42:33 回复(0)

select 
distinct 
user_id from
(
select *, count(*) over(partition by user_id) as num
from order_info
where product_name in ('C++','Java','Python')
and status='completed'
and date >'2025-10-15'
)
t
where t.num>=2
order by user_id
发表于 2022-08-22 21:02:12 回复(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(1) >= 2
order by user_id
发表于 2022-08-18 11:49:59 回复(0)
select distinct r.user_id 
from
(select user_id,count(*) over(partition by user_id) t
from 
order_info
where datediff(date,'2025-10-15')>0
and status='completed'
and product_name in ('C++','Java','python')) r
where r.t>=2
order by r.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(user_id)>=2
order by user_id

发表于 2022-08-11 16:26:10 回复(0)