首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(三)
  • 热度指数:94135 时间限制: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 *
from order_info
where user_id in 
(select user_id
from order_info
where status = 'completed' and date > '2025-10-15' and product_name in ('C++','Python','Java')
group by user_id
having count(*) > 1) and status = 'completed' and date > '2025-10-15' and product_name in ('C++','Python','Java')
order by id
一个平庸的想法,前一题找到了符合条件的user_id,所以自然想到让user_id in之前的表,但是这里的状态、时间以及课程条件需重复一次
发表于 2025-01-07 13:57: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++","Python","Java")
            group by user_id
            having count(*) > 1
            order by user_id )
        and date>"2025-10-15" 
        and status = "completed"
        and product_name in ("C++","Python","Java")

发表于 2024-10-23 11:21:02 回复(0)
SELECT
    id,
  user_id,
    product_name,
    STATUS,
    client_id,
    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 IN ( "C++", "Java", "Python" ) 
    ) AS a 
WHERE
    c > 1 
ORDER BY
    id
发表于 2024-09-05 16:00:57 回复(0)

考验 子查询 + group by + having
注意在子查询后,仍然需要加入对应的date、product_name、status条件哦

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 date >= '2025-10-15'
and product_name in ('C++', 'Java', 'Python')
and status = 'completed'
order by id asc
发表于 2024-08-13 17:56:50 回复(0)
select *
from order_info
where user_id in
(
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(id)>=2
order by user_id
)
and datediff(date, '2025-10-15')>0
and product_name in ('C++', 'Java', 'Python')
and status='completed'
order by id;

发表于 2024-07-29 23:59:26 回复(0)
手敲了一版答案,但是感觉判断了两次条件有点多余,求大佬更简化的方案;
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(id)>=2
    AND product_name in ('C++','Java','Python')
    AND status='completed'
    AND date > '2025-10-15'
    )
ORDER BY
    id ASC;

发表于 2024-07-07 20:22:10 回复(0)
select
    id,user_id,product_name,status,client_id,date
from (
    select
        *,
        count(id) over (partition by user_id) cnt
    from order_info
    where date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python')
) t
where t.cnt>=2
order by id
发表于 2024-06-10 09:11:23 回复(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 date >'2025-10-15' and 
product_name in('C++','Java','Python') and
status ='completed'
group by user_id
having count(*)>=2)

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

发表于 2024-05-29 13:32:25 回复(0)
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)