MySQL WITH ROLLUP没有显示我的预期

时间:2012-07-26 11:50:53

标签: mysql rollup

我正在尝试向我的SELECT查询添加一些总计,但我很难理解为什么这不起作用:

SELECT client, job_type, actual_value_fee FROM jo2details GROUP BY client, job_type WITH ROLLUP

以下是预期的内容:

+--------+----------+------------------+
| client | job_type | actual_value_fee |
+--------+----------+------------------+
| 110    | 2        | 1250             |
| 110    | 20       | 200              |
| 110    |          | 1450             |
| 228    | 27       | 1000             |
| 228    |          | 1000             |
| 229    | 32       | 0                |
| 229    |          | 0                |
|        |          | 2450             |
+--------+----------+------------------+

但这是我得到的:

+--------+----------+------------------+
| client | job_type | actual_value_fee |
+--------+----------+------------------+
| 110    | 2        | 1250             |
| 110    | 20       | 200              |
| 110    |          | 200              |
| 228    | 27       | 1000             |
| 228    |          | 1000             |
| 229    | 32       | 0                |
| 229    |          | 0                |
|        |          | 0                |
+--------+----------+------------------+

这是表格:

CREATE TABLE `jo2details` (
  `ref` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ID` int(11) unsigned NOT NULL,
  `client` int(11) unsigned NOT NULL,
  `job_type` int(11) unsigned DEFAULT NULL,
  `actual_value_fee` decimal(11,2) DEFAULT NULL,
  `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ref`),
  UNIQUE KEY `ref` (`ref`,`ID`),
  UNIQUE KEY `ref_2` (`ref`,`ID`,`client`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=413 ;

所有人都非常感谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT client, job_type, sum(actual_value_fee)
 FROM jo2details 
 GROUP BY client, job_type WITH ROLLUP