舞哈哈哈:使用MySQL做的,个人理解
select t1.order_add_user, t1.num, count(distinct t2.num)+1 as rank,
(case when count(distinct t2.num)+1<=3 then t1.num*50+1000 else t1.num*50 end) as bonus
from
(select order_add_user, sum(quantity) as num, date_format(order_add_time,"%Y-%m") as month
from order
where order_add_time<=date_format(now(),"%YYYY-%mm-%dd") and
order_add_time>date_format(now(),"%Y-%m-01")
group by order_add_user
order by num desc
limit 10 ) t1
left join
(select order_add_user, sum(quantity) as num, date_format(order_add_time,"%Y-%m") as month
from order
where order_add_time<=date_format(now(),"%YYYY-%mm-%dd") and
order_add_time>date_format(now(),"%Y-%m-01")
group by order_add_user
order by num desc
limit 10 ) t2 on t1.num<t2.num
group by t1.order_add_user
order by t1.num desc;
0 点赞 评论 收藏
分享
关注他的用户也关注了: