列表

详情


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

描述

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

有一个订单信息表(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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3584KB, 提交时间: 2022-01-26

select id, user_id, product_name, status, client_id, date from
   (select *, count() over(partition by user_id) num from order_info
   where product_name in ('C++', 'Java', 'Python')
   and status = 'completed' and date > '2025-10-15')
where num >= 2
order by id

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3624KB, 提交时间: 2021-09-07

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(*)>=2
)
and date>'2025-10-15'
and status=='completed'
and product_name in ('C++','Java','Python')
order by id

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3672KB, 提交时间: 2021-08-07

 select * from order_info 
where date>'2025-10-15'
      and product_name in ("C++","Java","Python")
      and status="completed"
      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(1) >1
 )
 order by id asc
 

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3716KB, 提交时间: 2021-09-09

with b as (
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  id  
)
select *  from order_info 
where date >'2025-10-15' and status = 'completed' and  product_name in ('C++','Java','Python') 
and  user_id  in b
order  by  id ;







Sqlite 解法, 执行用时: 10ms, 内存消耗: 3756KB, 提交时间: 2021-09-08

select o1.*
from order_info o1,(
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 ) o2
where o1.user_id=o2.user_id and date > '2025-10-15' and status ='completed' and product_name in('C++','Java','Python')
order by id

上一题