Это было совершенно запутанно и противно, но я понял
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>
Вот результат запроса:
mysql> 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;
+-------+-------+
| style | count |
+-------+-------+
| 1 | 2 |
| 2 | 3 |
| NULL | 5 |
+-------+-------+
3 rows in set (0.00 sec)
mysql>
Проблема в том, что WITH ROLLUP
вычисляется до HAVING
. Я организовал запрос таким образом, чтобы WITH ROLLUP
был выполнен последним.
Миссия выполнена !!!