比如取group by后每个分组的Top 31234567891011121314151617181920SELECT mac_group, mac, durFROM( SELECT mac_group, sum(sum_dur) as dur, mac, @rn := IF(@prev = mac_group and @prev_mac = mac, @rn + 1, 1) AS rn, @prev := mac_group, @prev_mac := mac FROM mac_customer_stats JOIN (SELECT @prev := NULL, @prev_mac :=NULL, @rn := 0) AS vars where mac_group in (1,2,3) group by mac_group, mac, day ORDER BY mac_group, mac, dur DESC) AS T1WHERE rn <= 3;