在按多个领域分类时,MySQL回到每个集团的滚动行以及总摘要:
CREATE TABLE test (name VARCHAR(50), number TINYINT);
INSERT INTO test VALUES
( foo , 1), ( foo , 1), ( foo , 2), ( foo , 3), ( foo , 3),
( bar , 1), ( bar , 2), ( bar , 2), ( bar , 2), ( bar , 3),
( baz , 1), ( baz , 2), ( bar , 2);
SELECT name, number, COUNT(1) FROM test GROUP BY name, number WITH ROLLUP;
+------+--------+----------+
| name | number | count(1) |
+------+--------+----------+
| bar | 1 | 1 |
| bar | 2 | 3 |
| bar | 3 | 1 |
| bar | NULL | 5 |
| baz | 1 | 1 |
| baz | 2 | 2 |
| baz | NULL | 3 |
| foo | 1 | 2 |
| foo | 2 | 1 |
| foo | 3 | 2 |
| foo | NULL | 5 |
| NULL | NULL | 13 |
+------+--------+----------+
I m not interested in the rollups for foo/bar/baz, only the overall summary. What s the most efficient way to achieve this?
我知道,由于随后添加滚动装置,我可以使用<代码>HAVING。 选择名称和编号为的最好办法。 NOT NUL
or both NUL
?