我想通过mysql查询从每个组中选择3条记录我的表结构是这样的 -
id | customer | catId
---------------------
1 | Joe | 2
2 | Sally | 2
3 | Joe | 2
4 | Sally | 2
5 | Joe | 2
6 | Sally | 3
7 | Joe | 3
8 | Sally | 3
9 | Joe | 3
10 | Sally | 4
11 | Joe | 4
12 | Sally | 4
我想为每个不同的 catId 方法选择 3 条记录
id | customer | catId
---------------------
1 | Joe | 2
2 | Sally | 2
3 | Joe | 2
6 | Sally | 3
7 | Joe | 3
8 | Sally | 3
10 | Sally | 4
11 | Joe | 4
12 | Sally | 4
我尝试了这个查询,但它只为每个不同的 catId 显示一条记录。l
SELECT * FROM
table
GROUP BY CatIds
我得到
id | customer | catId
---------------------
1 | Joe | 2
6 | Sally | 3
10 | Sally | 4