首页 > 试题广场 >

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

[编程题]牛客的课程订单分析(六)
  • 热度指数:85561 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

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

有一个订单信息表(order_info),简况如下:

id user_id product_name status client_id date is_group_buy
1 557336 C++ no_completed 1 2025-10-10 No
2 230173543 Python completed
2 2025-10-12
No
3 57 JS completed
0 2025-10-23
Yes
4 57 C++
completed
3 2025-10-23
No
5 557336 Java completed
0 2025-10-23
Yes
6 57 Java
completed
1 2025-10-24
No
7 557336
C++
completed
0 2025-10-25
Yes

1行表示user_id557336的用户在2025-10-10的时候使用了client_id1的客户端下了C++课程的非拼团(is_group_buyNo)订单,但是状态为没有购买成功。

2行表示user_id230173543的用户在2025-10-12的时候使用了client_id2的客户端下了Python课程的非拼团(is_group_buyNo)订单,状态为购买成功。

。。。

最后1行表示user_id557336的用户在2025-10-25的时候使用了下了C++课程的拼团(is_group_buyYes)订单,拼团不统计客户端,所以client_id所以为0,状态为购买成功。

有一个客户端表(client)简况如下:

id name
1 PC
2 Android
3 IOS
4 H5

请你写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的订单id是否拼团以及客户端名字信息,最后一列如果是非拼团订单,则显示对应客户端名字,如果是拼团订单,则显示NULL,并且按照order_infoid升序排序,以上例子查询结果如下:

id is_group_buy client_name
4 No IOS
5 Yes NULL
6 No
PC
7 Yes
NULL

解析:

id为46的订单满足以上条件,且因为4是通过IOS下单的非拼团订单输出对应的信息,6是通过PC下单的非拼团订单输出对应的信息以及客户端名字;

id为57的订单满足以上条件,且因为57都是拼团订单,输出对应的信息以及NULL;

按照id升序排序

示例1

输入

drop table if exists order_info;
drop table if exists client;
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,
is_group_buy varchar(32) NOT NULL,
PRIMARY KEY (id));

CREATE TABLE client(
id int(4) NOT NULL,
name varchar(32) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO order_info VALUES
(1,557336,'C++','no_completed',1,'2025-10-10','No'),
(2,230173543,'Python','completed',2,'2025-10-12','No'),
(3,57,'JS','completed',0,'2025-10-23','Yes'),
(4,57,'C++','completed',3,'2025-10-23','No'),
(5,557336,'Java','completed',0,'2025-10-23','Yes'),
(6,57,'Java','completed',1,'2025-10-24','No'),
(7,557336,'C++','completed',0,'2025-10-25','Yes');

INSERT INTO client VALUES
(1,'PC'),
(2,'Android'),
(3,'IOS'),
(4,'H5');

输出

4|No|IOS
5|Yes|None
6|No|PC
7|Yes|None
1。用左连接,
with t1 as(
    select
        o.id,is_group_buy,
        name as client_name,
        user_id,
        count(1)over(partition by user_id) as ci
    from order_info as o 
    LEFT JOIN client as c on c.id = o.client_id
    where
        date >'2025-10-15'
        and product_name in('Java','C++','Python') 
        and status = 'completed'
)
select 
    id,
    is_group_buy,
    client_name
from 
    t1
where 
    ci >1
order by 
    id asc
2 用 CASE WHEN
(case when o.client_id in(1,2,3,4) then c.name 
when c.name='None' then NULL end) as client_name



发表于 2025-05-07 21:17:32 回复(0)
select
    t1.id,
    t1.is_group_buy,
    c.name
from
    (
        select
            id,
            user_id,
            is_group_buy,
            client_id,
            count(product_name) over (
                partition by
                    user_id
            ) cnt
        from
            order_info
        where
            date > '2025-10-15'
            and product_name in ('C++', 'Java', 'Python')
            and status = 'completed'
    ) t1
    left join client c on t1.client_id = c.id
where
    t1.cnt >= 2
order by
    t1.id

发表于 2025-01-09 16:01:57 回复(0)
select
    o.id,o.is_group_buy,c.name
from
    order_info o
left join 
    client c
    on o.client_id = c.id
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
                )
    and date > "2025-10-15"
    and status = "completed"
    and product_name in ("C++", "Python", "Java")
order by
    o.id

发表于 2024-10-23 15:44:32 回复(0)
select
    a.id,
    a.is_group_buy,
    a.client_name
from
    (
        select
            o.id,
            o.is_group_buy,
            (
                case
                    when o.is_group_buy = 'No' then c.name
                    else NULL
                end
            ) as client_name,
            count(*) over (
                partition by
                    user_id
            ) as rk
        from
            order_info o
            left join client c on o.client_id = c.id
        where
            status = 'completed'
            and date > '2025-10-15'
            and product_name in ('C++', 'Java', 'Python')
    ) a
where
    a.rk >= 2
order by
    a.id
发表于 2024-09-18 14:38:35 回复(0)

子查询+if判断

select t1.id, t1.is_group_buy, if(t1.is_group_buy = 'Yes', NULL, t2.name) as client_name
from (
    select *
    from order_info 
    where 
    product_name in ('C++', 'Java', 'Python')
    and date >= '2025-10-15'
    and status = 'completed'
    and user_id in (
        --  符合条件的user_id
        select user_id
        from order_info 
        where 
        product_name in ('C++', 'Java', 'Python')
        and date >= '2025-10-15'
        and status = 'completed'
        group by user_id
        having count(id) >= 2
    )
) t1 left join client t2 on t1.client_id = t2.id
order by t1.id asc
发表于 2024-08-14 11:53:55 回复(0)
select
t.id,t.is_group_buy,
case when t.is_group_buy ='No' then t1.name
else null end client_name
from (
select
id,user_id,client_id,is_group_buy,
count(user_id)over(partition by user_id) cnt
from order_info
where date>='2025-10-15'
and status='completed'
and product_name in ('C++','Python','Java')
)t
left join client t1
on t.client_id=t1.id
where t.cnt>1
order by t.id
;
发表于 2024-07-20 13:34:49 回复(0)
有没有大佬,帮我看看SQL276 牛客的课程订单分析(六),我的错在哪,测试用例不完全通过
SELECT T1.id,T1.is_group_buy,
    CASE
        WHEN T1.is_group_buy = 'No' THEN T3.name
        ELSE NULL
    END AS client_name
FROM order_info AS T1
LEFT JOIN client AS T3 ON T1.client_id = T3.id
LEFT JOIN (
SELECT user_id,COUNT(user_id) AS num
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) AS T2
ON T1.user_id = T2.user_id
WHERE T1.date > '2025-10-15'
AND T1.status = 'completed'
AND T1.product_name IN ('C++','Java','Python')
ORDER BY T1.id ASC


发表于 2024-07-19 21:30:03 回复(0)
对于如何实现client_id 到 client_name的转换,两种方法
-- 子查询传入统计范围内的订单id、is_gropu_by、client_id
SELECT
    t1.id,
    t1.is_group_buy,
    CASE t1.client_id  --通过case转换 id -name
        WHEN 1 THEN 'PC' 
        WHEN 2 THEN 'Android'
        WHEN 3 THEN 'IOS'  
        WHEN 4 THEN 'H5' 
        ELSE NULL
    END
FROM
    (select
        id,
        user_id,
        is_group_buy,
        client_id,
        count(id) over(partition by user_id)  as cnt
    from
        order_info
    where
        date > '2025-10-15'
        and status ='completed'
        and product_name in('C++','Java','Python')
    ) t1 
WHERE
    t1.cnt>=2
ORDER BY
    t1.id asc;    
以及连接查询
SELECT
    t1.id,
    t1.is_group_buy,
    t2.name as client_name
FROM
    (select
        id,
        user_id,
        is_group_buy,
        client_id,
        count(id) over(partition by user_id)  as cnt
    from
        order_info
    where
        date > '2025-10-15'
        and status ='completed'
        and product_name in('C++','Java','Python')
    ) t1 
    left join  client t2 
    on t1.client_id = t2.id
WHERE
    t1.cnt>=2
ORDER BY
    t1.id asc;


发表于 2024-07-08 09:39:18 回复(0)
select t.id,t.is_group_buy,if(is_group_buy='No',c.name,null) as client_name
from (
    select *,count(id) over(partition by user_id) as num
    from order_info
    where date > '2025-10-15'
    and status = 'completed'
    and product_name in ('C++','Java','Python')
) t
left join client c on t.client_id = c.id
where num >= 2
order by id asc

发表于 2024-04-18 12:39:55 回复(0)
select o.id,o.is_group_buy,c.name as client_name
from order_info o left join client c on o.client_id=c.id
where (
select count(o.id)
from order_info
where o.product_name in ('C++','Python','Java') and o.date>'2025-10-15' and o.status="completed" and o.user_id=user_id
)>=2
order by o.id asc
4/5组通过,为什么,请问最后一组到底是因为什么无法通过
发表于 2024-04-13 20:47:51 回复(0)
SELECT a.id, a.is_group_buy, (CASE a.is_group_buy WHEN 'Yes' THEN NULL else b.name end) AS client_name
FROM (
    SELECT *, (count(*) over (partition by user_id))as cnt
    FROM order_info    
    WHERE order_info.`date`>'2025-10-15' and order_info.status = 'completed' 
    and order_info.product_name in ('C++', 'Java', 'Python')
    ) a
LEFT JOIN client b on a.client_id = b.id
WHERE a.cnt>1
order by a.id

发表于 2024-03-07 00:12:56 回复(0)
WITH t2 AS (
    SELECT
        t1.id,
        oi.client_id,
        oi.is_group_buy 
    FROM
        (
        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 
            )) t1
        JOIN order_info oi ON t1.id = oi.id 
    ) -- select * from t2
SELECT
    t2.id,
    t2.is_group_buy,
CASE
        WHEN is_group_buy = 'NO' THEN
        c.NAME ELSE NULL 
    END AS client_name 
FROM
    t2
    LEFT JOIN client c ON t2.client_id = c.id
发表于 2024-03-03 21:10:12 回复(0)
select oi.id,is_group_buy,c.name from order_info as oi

inner join 
(select user_id as uid from order_info
where status='completed'
and product_name in ('C++','Java','Python')
and datediff(date,'2025-10-15')>0
group by uid
having count(uid)>1) as t1 #购买两次的用户表
on t1.uid=oi.user_id

left join client as c on c.id=oi.client_id

where status='completed'
and product_name in ('C++','Java','Python')
and datediff(date,'2025-10-15')>0
order by oi.id;


发表于 2024-01-13 18:37:52 回复(0)
select aa.id,aa.is_group_buy,
(case aa.is_group_buy when 'Yes' then NULL else bb.name end) client_name
from 
(select
a.id,a.is_group_buy,a.client_id
from order_info a
where a.date>'2025-10-15'
and a.product_name in ('Java','Python','C++')
and a.status='completed'
and a.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)) aa
left join client bb
on aa.client_id=bb.id
order by aa.id

发表于 2024-01-02 11:38:24 回复(0)
select t1.id,t1.is_group_buy,b.name from  client b RIGHT JOIN
(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++','Java','Python') group by user_id having count(1)>1 ) and product_name in('C++','Java','Python') and status='completed' and date>'2025-10-15' order by id)t1 on b.id=t1.client_id ;
发表于 2024-01-01 17:09:55 回复(0)
select
    g.id,
    is_group_buy,
    case when client_id=0 then NULL else name end client_name
from
    (
        select
            *,
            count(product_name) over (
                partition by
                 user_id
            )rk
        from
            order_info
        where
            date > '2025-10-15'
            and status = 'completed'
            and product_name in ('C++', 'Java', 'Python')
    ) g

left join client c on g. client_id=c.id
where rk>=2
order by id
发表于 2023-11-24 14:40:33 回复(0)

为什么我这种写法通不过,哪里有问题,求指教

with
    t1 as (
        select
            `user_id`,
            count(id) c
        from
            order_info
        group by
            user_id
        having
            c > 1
    )
select
    order_info.id,
    is_group_buy,
    if (is_group_buy = 'No', c.name, NULL) client_name
from
    order_info
    inner join t1 on t1.user_id = order_info.user_id
    left join client c on c.id = client_id
where
    date > '2025-10-15'
    and status = 'completed'
    and product_name in ('C++', 'Python', 'Java')
order by
    order_info.id


发表于 2023-11-20 17:43:28 回复(0)
with t as (
    select
        o.id,
        is_group_buy,
        name client_name,
        count(o.id) over(partition by user_id) cnt
    from order_info o
    left join client c
    on o.client_id = c.id
    where date > '2025-10-15'
    and status = 'completed'
    and product_name in ('C++','Python','Java')
)
select id,is_group_buy,client_name from t where cnt >= 2 order by id;

发表于 2023-11-14 11:16:40 回复(0)
select a.id,is_group_buy, client_name
from (
select a.id,is_group_buy,if(is_group_buy='No',name,null)as client_name,
count(*)over(partition by user_id) as cnt
from order_info a left join client b on a.client_id=b.id
where date>'2025-10-15' and status='completed' and product_name in ('C++','Python','Java') ) a
where cnt>=2
order by a.id

发表于 2023-10-16 16:07:19 回复(0)