我想通过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显示了一条记录。 升
SELECT * FROM
table
GROUP BY CatIds
我正在
id | customer | catId
---------------------
1 | Joe | 2
6 | Sally | 3
10 | Sally | 4
答案 0 :(得分:-2)
您可以改为运行3个查询。
SELECT * FROM table WHERE catId=2 LIMIT 3
SELECT * FROM table WHERE catId=3 LIMIT 3
SELECT * FROM table WHERE catId=4 LIMIT 3