首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(三)
  • 热度指数:83143 时间限制: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 o.*
from (
    select user_id,count(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
) t
left join order_info o
on t.user_id = o.user_id
where date > '2025-10-15'
and status = 'completed'
and product_name in ('C++','Java','Python')
order by id asc
好像做头铁了
发表于 2024-04-18 12:17:48 回复(1)
select *
from (
    select * 
    from order_info
    where status="completed" and date>'2025-10-15' and product_name in ('Java','C++','Python')
    ) as u

where (
    select count(id)
    from order_info as n
    where n.status="completed" and n.date>'2025-10-15' and n.product_name in ('Java','C++','Python') and u.user_id=n.user_id
)>=2
order by id ascz
这道题与(一)解法类似,一个子查询在from一个在where:
where中想法不变为了选择在from表中 大于等于2 的记录,
from中子查询是因为结果 只需要满足条件记录 我这里选择先筛选

发表于 2024-04-13 19:03:33 回复(0)
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 order_info
where date>'2025-10-15'
and product_name in ('Java','Python','C++')
and status='completed'
and user_id in 
(select user_id
from order_info 
where date>'2025-10-15'
and product_name in ('Java','Python','C++')
and status='completed'
group by user_id
having count(distinct product_name)>=2)
order by id 

发表于 2024-01-02 09:59:57 回复(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(user_id)>=2
)
and date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python')
order by id

----------------------------------------------------------------------------------------------------------------------------
select id,user_id,product_name,status,client_id,date from
(select *,count(user_id)over(partition by user_id) ct
from order_info
where date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python'))xb
where ct>=2
order by
发表于 2023-12-02 09:38:11 回复(0)
select id,user_id,product_name,status,client_id,date
from(
select *,count(*)over(partition by user_id) as num
from order_info
where date>'2025-10-15' and product_name in ('C++','Java','Python')
and status='completed' ) a
where num>=2
order by id

发表于 2023-10-16 14:38:35 回复(0)
还行
select c.* from 
(select user_id,count(user_id) as sl from 
(select *  from order_info
where status='completed' and date>'2025-10-15' and product_name in ('C++','Java','Python')
) a 
group by user_id) b

inner join 
(select *  from order_info
where status='completed' and date>'2025-10-15' and product_name in ('C++','Java','Python')) c
using(user_id)
where b.sl>=2


发表于 2023-08-02 09:18:59 回复(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(user_id) >= 2
            )
    and date > '2025-10-15'
    and status = 'completed'
    and product_name in ('C++','Python','Java')
order by
    id

发表于 2023-05-25 17:00:45 回复(0)
# 2025-10-15以后,同一个用户下单2个及2个以上状态为购买成功的C++/Java/Python

# 先筛一遍,按照"2025-10-15以后,C++/Java/Python,状态为购买成功"进行过滤
with order_info_filtered as( 
    select * 
    from order_info
    where date>"2025-10-15" and
    product_name in ("C++","Java","Python") and
    status="completed"
)
,
# 找到满足条件的user_id
specified_user_id as(
    select user_id,count(*) as cnt
    from  order_info_filtered
    group by user_id
    having cnt>=2
)

select * from order_info_filtered
where user_id in (select distinct user_id from specified_user_id)
order by id asc

发表于 2023-01-28 09:57:46 回复(0)
select id,user_id,product_name,status,client_id,date
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(*) >= 2
   order by user_id
)

发表于 2023-01-01 15:54:02 回复(0)
SELECT * FROM
order_info
WHERE date > '2025-10-15'
AND `status` = 'completed'
AND product_name in('Python','C++','Java')
AND user_id 
in(SELECT user_id FROM
order_info
WHERE date > '2025-10-15'
AND `status` = 'completed'
AND product_name in('Python','C++','Java')
GROUP BY user_id
HAVING COUNT(1) >= 2)
ORDER BY id
发表于 2022-12-01 20:56:55 回复(0)
方法一:用子查询
select id,user_id,product_name,status,client_id,`date`
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 sum(status='completed')>=2
    order by user_id
) 
and `date`>'2025-10-15' 
and product_name in ('C++','Java','Python') 
and status = 'completed'

实在搞不懂为啥子查询里面用了这个限定条件
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 (
    select id,user_id,product_name,client_id,status,date,
    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'
)t
where t.num>=2
order by id asc;

发表于 2022-11-16 19:51:44 回复(0)
select *
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(*) >= 2
                )
        and `date` > '2025-10-15' and `status` = 'completed' and product_name in ('C++','Java','Python')
order by id asc
发表于 2022-09-16 04:24:30 回复(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(1)>=2
            )
and status = 'completed'
and product_name in ('C++','Java','Python')
and date>'2025-10-15'
order by id
发表于 2022-09-14 10:36:29 回复(0)
select r.id,r.user_id,r.product_name,r.status,r.client_id,r.date
from
(select *,count(*) over(partition by user_id) t
from order_info
where product_name in ('C++','Java','Python')
and datediff(date,'2025-10-15')>0
and status='completed') r 
where r.t>=2
order by r.id

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

发表于 2022-07-12 14:42:22 回复(0)
使用窗口函数
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)
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(user_id) > 1
  )
  and date > '2025-10-15'
  and status = 'completed'
  and product_name in('C++', 'Java', 'Python')
过是过了,感觉怪怪的……
发表于 2022-06-25 22:02:57 回复(0)