数据库每日一题:
orders表有三个字段:id(订单编号)、customer_id(客户编号)和order_time(下单日期),在表orders中找到订单数最多客户对应的customer_id,具体如下图所示:
参考答案(有更优SQL留言共同学习):
select o.customer_id
from
(select customer_id, count(customer_id) as total from orders group by customer_id)
as o
where o.total=
(select max(o.total)
from
(select count(customer_id) as total from orders group by customer_id)
as o)