Mysql rollup通过具有约束忽略组

Mysql rollup通过具有约束忽略组,mysql,group-by,having,rollup,Mysql,Group By,Having,Rollup,查询: +----+-------+-------+ | id | style | color | +----+-------+-------+ | 1 | 1 | red | | 2 | 1 | blue | | 3 | 2 | red | | 4 | 2 | blue | | 5 | 2 | green | | 6 | 3 | blue | +----+-------+-------+ 产生: SELECT styl

查询:

+----+-------+-------+
| id | style | color |
+----+-------+-------+
|  1 |     1 | red   |
|  2 |     1 | blue  |
|  3 |     2 | red   |
|  4 |     2 | blue  |
|  5 |     2 | green |
|  6 |     3 | blue  |
+----+-------+-------+
产生:

SELECT style, COUNT(*) as count from t GROUP BY style WITH ROLLUP HAVING count > 1;

我必须做些什么才能使用汇总来只对那些满足要求的计数求和?也就是说,我想在汇总行中看到“5”表示计数。

这完全是令人费解和讨厌的,但我明白了

+-------+-------+
| style | count |
+-------+-------+
|     1 |     2 |
|     2 |     3 |
|  NULL |     6 |
+-------+-------+
以下是问题的样本数据:

SELECT style,COUNT(1) as count
FROM t
WHERE NOT EXISTS
(
    SELECT t1.style as count
    FROM
    (
        SELECT style from t GROUP BY style HAVING count(*) = 1
    ) t1 WHERE t.style = t1.style
)
GROUP BY style
WITH ROLLUP;
此处加载:

drop database if exists rollup_test;
create database rollup_test;
use rollup_test
create table t (id int not null auto_increment,
style int,color varchar(10),primary key (id));
insert into t (style,color) values
(1,'red'),(1,'blue'),(2,'red'),
(2,'blue'),(2,'green'),(3,'blue');
select * from t;
以下是查询的结果:

mysql> drop database if exists rollup_test;
Query OK, 1 row affected (0.07 sec)

mysql> create database rollup_test;
Query OK, 1 row affected (0.00 sec)

mysql> use rollup_test
Database changed
mysql> create table t (id int not null auto_increment,
    -> style int,color varchar(10),primary key (id));
Query OK, 0 rows affected (0.10 sec)

mysql> insert into t (style,color) values
    -> (1,'red'),(1,'blue'),(2,'red'),
    -> (2,'blue'),(2,'green'),(3,'blue');
Query OK, 6 rows affected (0.05 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from t;
+----+-------+-------+
| id | style | color |
+----+-------+-------+
|  1 |     1 | red   |
|  2 |     1 | blue  |
|  3 |     2 | red   |
|  4 |     2 | blue  |
|  5 |     2 | green |
|  6 |     3 | blue  |
+----+-------+-------+
6 rows in set (0.00 sec)

mysql>
问题在于,使用汇总时在使用之前进行评估。我以这样的方式安排查询:最后完成了WITHROLLUP


任务完成

这完全是一个令人费解和讨厌的问题,但我明白了

+-------+-------+
| style | count |
+-------+-------+
|     1 |     2 |
|     2 |     3 |
|  NULL |     6 |
+-------+-------+
以下是问题的样本数据:

SELECT style,COUNT(1) as count
FROM t
WHERE NOT EXISTS
(
    SELECT t1.style as count
    FROM
    (
        SELECT style from t GROUP BY style HAVING count(*) = 1
    ) t1 WHERE t.style = t1.style
)
GROUP BY style
WITH ROLLUP;
此处加载:

drop database if exists rollup_test;
create database rollup_test;
use rollup_test
create table t (id int not null auto_increment,
style int,color varchar(10),primary key (id));
insert into t (style,color) values
(1,'red'),(1,'blue'),(2,'red'),
(2,'blue'),(2,'green'),(3,'blue');
select * from t;
以下是查询的结果:

mysql> drop database if exists rollup_test;
Query OK, 1 row affected (0.07 sec)

mysql> create database rollup_test;
Query OK, 1 row affected (0.00 sec)

mysql> use rollup_test
Database changed
mysql> create table t (id int not null auto_increment,
    -> style int,color varchar(10),primary key (id));
Query OK, 0 rows affected (0.10 sec)

mysql> insert into t (style,color) values
    -> (1,'red'),(1,'blue'),(2,'red'),
    -> (2,'blue'),(2,'green'),(3,'blue');
Query OK, 6 rows affected (0.05 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from t;
+----+-------+-------+
| id | style | color |
+----+-------+-------+
|  1 |     1 | red   |
|  2 |     1 | blue  |
|  3 |     2 | red   |
|  4 |     2 | blue  |
|  5 |     2 | green |
|  6 |     3 | blue  |
+----+-------+-------+
6 rows in set (0.00 sec)

mysql>
问题在于,使用汇总时在使用之前进行评估。我以这样的方式安排查询:最后完成了WITHROLLUP

任务完成