mysql选择LIMIT by字段值

时间:2012-11-09 17:54:29

标签: mysql

  

可能重复:
  mysql: Using LIMIT within GROUP BY to get N results per group?

我尝试了group by,但之后每个account_id只能获得1个,我想要每个account_id 10个。这就是我要找的东西。

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

1 个答案:

答案 0 :(得分:0)

您可以为每个account_id,id组

使用排名

示例:

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