像这样:
SELECT id, `desc`, value FROM table
UNION ALL
SELECT MAX(id), 'Total', SUM(value) FROM table;
但是,如果您需要将表中的选择限制为仅 5 个,则必须LIMIT
在两个子查询中使用,如下所示:
SELECT id, `desc`, value
FROM
(
SELECT id, `desc`, value FROM table1
ORDER BY id
LIMIT 5
) t
UNION ALL
SELECT MAX(id), 'Total', SUM(value)
FROM
(
SELECT id, `desc`, value FROM table1
ORDER BY id
LIMIT 5
) t;
对于您的示例数据,这将为您提供:
| ID | DESC | VALUE |
----------------------
| 1 | A | 100 |
| 2 | B | 200 |
| 3 | C | 300 |
| 4 | D | 400 |
| 5 | E | 500 |
| 5 | Total | 1500 |
请注意:该Total
行将是所有先前value
值的总和,但是,在您的样本数据中,它不是总数。