- 参考答案
select if(oi.client_id=0,'GroupBuy',c.name) as 'source' ,
count(oi.client_id) as cnt
from order_info oi left join client c
on oi.client_id = c.id
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 o
where date>'2025-10-15'
and status='completed'
and product_name in ('C++','Java','Python')
group by user_id
having count(o.id)>1)
group by oi.client_id
order by source
- 答案解析
本题解决关键是以下两段代码
第一段代码帅选出了符合条件的user_id,
第二段代码分组,从而得到我们想要的数据
and user_id in
(select user_id from order_info o
where date>'2025-10-15'
and status='completed'
and product_name in ('C++','Java','Python')
group by user_id
having count(o.id)>=2)
和
group by oi.client_id