题解 | IF函数+FROM子句中的子查询+窗口函数+外连接JOIN
牛客的课程订单分析(六)
http://www.nowcoder.com/practice/c5736983c322483e9f269dd23bdf2f6f
SELECT new.id, new.is_group_buy, IF(new.is_group_buy = 'Yes', NULL, c.name)
FROM(
select *,
count(*)over(partition by user_id ) as count
-- 基于题目订单分析三,使用窗口函数更简洁
FROM order_info
WHERE date >= '2025-10-15'
AND status ='completed'
AND product_name in('C++','Python','Java')
) new
-- FROM子句中的派生表必须重新命名,否则会报错
LEFT JOIN client c ON new.client_id = c.id
-- new表中存在client_id = 0的数据,但client表中没有,所以这里必须使用左连接,否则返回结果不全
WHERE new.count >= 2
ORDER BY id