67,541
社区成员
发帖
与我相关
我的任务
分享
select a.merchantid, a. goodsname, b.price, b.sum_sale,
1+(select count(*)
from goods aa, goods_price bb
where aa.id = bb.goods_id
and aa.merchantid = a.merchantid
and bb.sum_sale > b.sum_sale) as rank
from goods a, goods_price b
where a.id = b.good_id
and rank <= 3
order by a.merchantid, b.sum_sale desc, b.price
大佬厉害了 太难了 我昨天用的这样的 使用group+GROUP_CONCAT来聚合数据在一排的,然后用SUBSTRING_INDEX来获取前三条,最后的数据格式是这样的
用逗号分隔,商品的详情数据一排使用逗号分隔,然后在程序里切割组合数据的

select * from (
select
if(@merchid = t.merchantid,@rank := @rank + 1,@rank := 1) as rank
,@merchid := t.merchantid as merchantid
,goods_id,goodsname,sum_sale,price
from (select @merchid := NULL,@rank := 0) r,(
select merchantid,goods_id,goodsname,sum_sale,price
from goods t1,goods_price t2 where t1.id = t2.goods_id
) t
ORDER BY t.merchantid,t.sum_sale desc,t.price desc
) tt
where tt.rank <= 3
;