我有一个N + 1问题想解决。我有N个这样的查询,我想在其中设置偏移量,限制和计数找到的行(所有查询中的限制和偏移量都相同):
students table:
|-----------|----------|-----------|
| id | name | group |
|-----------|----------|-----------|
| 1 | Bob | groupA |
|-----------|----------|-----------|
| 2 | Fred | groupB |
|-----------|----------|-----------|
| 3 | Bill | groupB |
|-----------|----------|-----------|
| 4 | Joe | groupC |
|-----------|----------|-----------|
| 5 | Bob | groupB |
|-----------|----------|-----------|
| 6 | Mark | groupB |
|-----------|----------|-----------|
| 7 | Will | groupC |
|-----------|----------|-----------|
| 8 | Tom | groupD |
|-----------|----------|-----------|
SELECT SQL_CALC_FOUND_ROWS id, name
FROM students
WHERE group = 'groupA'
ORDER BY name
LIMIT 3;
SELECT FOUND_ROWS() as count;
SELECT SQL_CALC_FOUND_ROWS id, name
FROM students
WHERE group = 'groupB'
ORDER BY name
LIMIT 3;
SELECT FOUND_ROWS() as count;
SELECT SQL_CALC_FOUND_ROWS id, name
FROM students
WHERE group = 'groupC'
ORDER BY name
LIMIT 3;
SELECT FOUND_ROWS() as count;
...and so on
是否存在一种有效的方法来聚合此查询并针对我的数据库执行这些查询,从而不会导致高负载并使用大量行和组快速解决问题?
在这种聚合查询的输出中,我希望接收每个选定组的数据集,并应用LIMIT和该组计数的总行