我需要在MySQL表上 SUBTOTAL 和 GRAN TOTAL 。
对于MySQL指南,我需要使用 WITH ROLLUP 语法。
我尝试了此SQL查询,但未成功:
SELECT
xName,
xCode,
sCode,
FBB88
FROM
`doTable`
GROUP BY
xCode,
FBB88 WITH ROLLUP;
输出:
+-----------+-------+--------+-------+
| xName | xCode | sCode | FBB88 |
+-----------+-------+--------+-------+
| AAAAAAAA | XX4A | XX4AU1 | 1 |
| AAAAAAAA | XX4A | XX4AU2 | 4 |
| BBBBBBBBB | XX4B | XX4BU1 | 2 |
| BBBBBBBBB | XX4B | XX4BU2 | 5 |
+-----------+-------+--------+-------+
4 rows in set
所需的输出:
+-----------+-------+--------+-------+
| xName | xCode | sCode | FBB88 |
+-----------+-------+--------+-------+
| AAAAAAAA | XX4A | XX4AU1 | 1 |
| AAAAAAAA | XX4A | XX4AU2 | 4 |
| AAAAAAAA | XX4A | SUBTOT | 5 |
| BBBBBBBBB | XX4B | XX4BU1 | 2 |
| BBBBBBBBB | XX4B | XX4BU2 | 5 |
| BBBBBBBBB | XX4B | SUBTOT | 7 |
| | | GRATOT | 12 |
+-----------+-------+--------+-------+
答案 0 :(得分:3)
您的分组依据不符合ANSI SQL,并且也会在最新版本的MySQL中引发错误。 Select
子句中的任何未聚合的列/表达式也必须在Group By
子句中指定。
请尝试以下操作:
SELECT
xName,
xCode,
sCode,
SUM(FBB88)
FROM
`doTable`
GROUP BY
xCode,
xCode,
sCode
WITH ROLLUP;