0

可能重复:
mysql:在 GROUP BY 中使用 LIMIT 来获得每组 N 个结果?

我试过group by了,但是每个account_id我只会得到1个,我想要每个account_id 10个。这就是我要找的东西。

SELECT id,account_id FROM whatever {LIMIT to 10 of each account_id}
4

1 回答 1

0

您可以对每个 account_id、id 组使用 rank

例子:

SELECT id,account_id FROM (
SELECT id,account_id,
   @acrank:=CASE 
               WHEN @aid <> id THEN 1 
               ELSE @acrank+1 END AS rn,
   @id:=id AS id_set
FROM
  (SELECT @acrank:= -1) nr,
  (SELECT @id:= -1) n,
  (SELECT * FROM Test ORDER BY id, account_id) t
  ) x WHERE rn < 11
于 2012-11-09T18:02:14.923 回答