SQL271. 牛客的课程订单分析(一)
描述
有很多同学在牛客购买课程来学习,购买会产生订单存到数据库里。
有一个订单信息表(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 | 557336 | Python | no_completed | 1 | 2025-10-24 |
第1行表示user_id为557336的用户在2025-10-10的时候使用了client_id为1的客户端下了C++课程的订单,但是状态为没有购买成功。
第2行表示user_id为230173543的用户在2025-10-12的时候使用了client_id为2的客户端下了Python课程的订单,状态为购买成功。
。。。
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 |
示例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,557336,'Python','no_completed',1,'2025-10-24');
输出:
4|57|C++|completed|3|2025-10-23 5|557336|Java|completed|1|2025-10-23
Sqlite 解法, 执行用时: 9ms, 内存消耗: 3372KB, 提交时间: 2021-08-09
select * from order_info where date>'2025-10-15' and status='completed' and product_name in('C++','Java','Python') order by id
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3368KB, 提交时间: 2021-09-08
select * from order_info where status='completed' and product_name in ('C++','Java','Python') and date>'2025-10-15' order by id;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3368KB, 提交时间: 2021-09-06
select * from order_info where date > '2025-10-15' and status = 'completed' and (product_name = 'C++' or product_name = 'Java' or product_name = 'Python') order by id;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3376KB, 提交时间: 2021-08-09
select * from order_info where date>'2025-10-15' and status ='completed' and product_name in ('C++','Java','Python') order by id
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3388KB, 提交时间: 2021-09-14
select * from order_info where status='completed' and product_name in ('Python','C++','Java') and date> '2025-10-15' order by id