67,543
社区成员




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
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
;