首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(三)
  • 热度指数:82325 时间限制: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课程的订单信息,并且按照order_infoid升序排序,以上例子查询结果如下:


id user_id product_name
status
client_id
date
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

解析:

id为46的订单满足以上条件,输出它们的对应的信息;

id为57的订单满足以上条件,输出它们的对应的信息;

按照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');

输出

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
select k.* from 
(select * from order_info where 
product_name in('C++','Java','Python') and
status='completed' and date>'2025-10-15') as k join  #先构建一个除了购买课程大于等于2的条件的表
(select y.user_id from (select user_id from order_info #再找出上述条件+购买课程大于等于2的 user_id  然后两表链接
where product_name in('c++','Java','Python')
and status='completed'
and date>'2025-10-15')as y
group by y.user_id
having count(y.user_id)>=2) as x on
k.user_id=x.user_id  
order by k.id
发表于 2021-04-20 18:40:52 回复(0)
select id,user_id,product_name,status,client_id,date
from (select id,user_id,product_name,status,client_id,date,
count(1) over(partition by user_id) num
from order_info
where date>'2025-10-15'
and status = 'completed'
and product_name in ('C++','Java','Python'))m
where num>=2
order by id
发表于 2021-02-27 22:37:33 回复(1)
时尚经典小错误合集
错误1-group by错误使用:
group by 只会返回第一行数据
select *
from order_info
where product_name in ('C++','Java','Python')
and status='completed'
and date >'2025-10-15'
group by user_id
having count(*)>=2
order by id
错误2-窗口函数错误使用:
窗口函数是对where或者group by子句处理后的结果进行操作,不可在一次查询中对窗口函数的结果进行操作
select t.id,t.user_id,t.product_name,t.status,t.client_id,t.date
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'
where num>=2)t
order by t.id
运行通过的答案:
select t.id,t.user_id,t.product_name,t.status,t.client_id,t.date
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 t.id


发表于 2021-04-07 12:52:09 回复(18)

解法一

不使用窗口函数

  1. 首先按照第二题的写法

    • 直接选择的后果是每个用户只保留有一个记录
    • 原因是group by之后只剩一条记录了```
      select *
      from order_info
      where date > '2025-10-15'
      and status = "completed"
      and product_name in ("C++", "Python", "Java")
      group by user_id
      having count(distinct product_name) >= 2
      order by user_id;
  2. 但是上面的写法可以得出题目所要的用户

    • 将*改成user_id,就得到我们所要的用户```
      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(distinct product_name) >= 2
      order by user_id;
  3. 最后就是正确解法了

    • 将所有条件选择好
    • 再选择所要的用户```
      select *
      from order_info
      where date > '2025-10-15'
      and status = 'completed'
      and product_name in ('C++', 'Java', 'Python')
      and user_id in (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(distinct product_name) >= 2
            )
      order by id;

解法二

使用窗口函数

select t.id, t.user_id, t.product_name, t.status, t.client_id, t.date
from (
    select *, count(*) over(partition by user_id) co
    from order_info
    where date > '2025-10-15'
    and status = 'completed'
    and product_name in ('C++', 'Java', 'Python')
) t
where t.co >= 2
order by t.id;
编辑于 2021-10-02 16:03:35 回复(5)
窗口函数
select id,user_id,product_name,status,client_id,date
from
(select *,count(*)over(partition by user_id) as count_num
from order_info
 where product_name in ('C++','Java','Python')
and status='completed'
and date >'2025-10-15'

)a    
where a.count_num >=2 
order by id asc
发表于 2021-03-02 14:28:39 回复(0)
思路:弄了两个子查询,一个是查简单条件的,然后关联购买两个成功的用户id,即可求得
select T.* 
from (
    select * 
    from order_info
    where date > '2025-10-15'
    and product_name in ('C++', 'Python', 'Java')
    and status = 'completed'
) T
where user_id in (
    select user_id
    from order_info
    where date > '2025-10-15'
    and product_name in ('C++', 'Python', 'Java')
    and status = 'completed'
    group by user_id
    having count(*) > 1
)
order by id


发表于 2021-04-06 22:03:08 回复(1)
查出一张表的user_id作为自连接表的判断条件,在进行查询即可,代码如下:
select *
from order_info
where user_id in
(select 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(user_id) > 1
)
and product_name in ('C++','Java','Python')
and status = 'completed'
and date > '2025-10-15'
order by id
发表于 2021-03-01 09:37:01 回复(7)
窗口函数:
select a.id,a.user_id,a.product_name,a.status,a.client_id,a.date 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.id


发表于 2021-08-18 18:52:58 回复(0)
select oo.* from order_info oo inner join
    (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) tmp
on tmp.user_id=oo.user_id
where 
date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python')
order by id
tmp表示78题的表  然后用order_info连接一下即可
发表于 2021-07-14 18:47:08 回复(0)
select * 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 id asc
请问下这个脚本有什么问题
发表于 2021-02-28 10:04:34 回复(4)
使用窗口函数
with temp as 
(select 
*,
count(user_id) 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'
)

select
id,user_id,product_name,
status,client_id,date
from temp
where num >= 2
order by id
发表于 2022-06-27 16:56:41 回复(0)
#思路  思路简单
1.利用开窗函数求得数量t_count,再用一次子查询求出数量大于1的结果集
select id,user_id,product_name,status,client_id,date
from (select id,user_id,product_name,status,client_id,date,count(user_id) over(partition by user_id order by user_id) as t_count
      from order_info
      where date > '2025-10-15'
      and status = 'completed'
      and product_name in ('C++','Python','Java')) a
where t_count>1
order by id


发表于 2022-04-19 15:53:55 回复(0)
哈哈哈看见一个时尚经典小错误:
1.分组后,没办法再拆开:group by 后每组只有一行
2.窗口函数,新建列的筛选,比如在表的外面
3.注意读题且理解:这里是总和为2,count(*),不能用row_number的话没办法筛选总和
4.一般这种很多的筛选条件的题目,都要建立临时表,并且临时表不能用分号结尾
with t as
(select *
 from order_info
 where date>'2025-10-15'
 and product_name in('C++','Java','Python')
 and status='completed'
)
select id,user_id,product_name,status,client_id,date
from
(select *,count(*)over(partition by user_id)as total
from t) as a
where total>1
order by id asc;
发表于 2021-04-07 17:05:22 回复(1)
select 
id
,user_id 
,product_name 
,status 
,client_id 
,date
from 
(
select
* 
,count(*)over(partition by user_id) c 
from order_info 
where date > '2025-10-15' and status = 'completed'
and product_name in ('C++','Java','Python')
) a 
where c >= 2
order by id
select
*
from order_info 
where user_id in 
(
    select
    user_id
    from order_info 
    where date > '2025-10-15' and status = 'completed'
    and product_name in ('C++','Java','python')
    group by 1
    having count(*) >= 2
)
and date > '2025-10-15' and status = 'completed'
and product_name in ('C++','Java','python')


发表于 2024-01-21 02:06:49 回复(0)
select id,user_id,product_name,status,client_id,date
from 
(
select *,
count(*) over(partition by user_id) as cnt
from order_info
where
product_name in('C++','Java','Python')
and 
status='completed'
and 
date>date('2025-10-15')
) as tb
where cnt>=2
order by id;

发表于 2023-03-06 11:56:36 回复(0)
select * from order_info
where user_id in (
    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(id)>=2
    order by user_id
)
and date > '2025-10-15'
and status = 'completed'
and product_name in ('C++','Java','Python');

发表于 2021-11-01 20:42:04 回复(4)
SELECT
	t.id,
	t.user_id,
	t.product_name,
	t.STATUS,
	t.client_id,
	t.date 
FROM
	(
	SELECT
		*,
		count( product_name ) over ( PARTITION BY user_id ) num 
	FROM
		order_info 
	WHERE
		date > '2025-10-15' 
		AND STATUS = 'completed' 
		AND product_name IN ( 'C++', 'Java', 'Python' ) 
	) t 
WHERE
	t.num >= 2 
ORDER BY
	t.id;

发表于 2021-08-24 11:58:41 回复(0)
SELECT order_info.* FROM order_info
JOIN 
    (SELECT user_id, COUNT(*) count FROM order_info
    WHERE date > '2025-10-15' AND status = 'completed' 
    AND product_name IN ('C++', 'Python', 'Java')
    GROUP BY user_id) t
ON order_info.user_id = t.user_id
WHERE t.count >= 2
    AND date > '2025-10-15' AND status = 'completed' 
    AND product_name IN ('C++', 'Python', 'Java')
ORDER BY id;


发表于 2021-08-24 04:31:40 回复(0)
select *
from order_info
where user_id in
    (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(product_name) >= 2)
and date > "2025-10-15"
and status = "completed"
and product_name in ("C++","Python","Java")
order by id;
发表于 2021-08-19 17:43:38 回复(2)
select *
from order_info 
where user_id in (
    select user_id
    from order_info 
    where product_name in ('C++','Python','Java')
    and `date` > '2025-10-15'
    and `status` = 'completed'
    GROUP by user_id
    HAVING count(user_id) >= 2
)
    and product_name in ('C++','Python','Java')
    and `date` > '2025-10-15'
    and `status` = 'completed'
ORDER by id asc

最直接的办法
发表于 2021-08-10 16:10:06 回复(0)