MySQL UNION ALL 运行 是如何聚合函数的?
How does MySQL UNION ALL run agregate functions?
我有一些 SQL 代码可以正常工作:
select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_10
union all
select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_11
union all
select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_12
group by callsign, number
order by max_ts
结果:
number,name,max(id),max_ts,callsign,max(time_hint_utc),count(*)
10,Checklist 10 Foo,2,1486554484635,VRTEST,2017-02-08 12:21:32,2
11,Checklist 11 Bar,2,1486554490674,VRTEST,2017-02-08 12:21:39,2
12,Checklist 12 Baz,2,1486554496378,VRTEST,2017-02-08 12:21:44,2
12,Checklist 12 Baz,3,1486554496379,VRTEST2,2017-02-08 12:21:45,1
特别是,我看到 max()
和 count()
的正确结果,即每个 callsign
/[=22 的 max
和 count
=] 组合,不是单独的 selects
.
这怎么行?聚合函数 看起来 就像它们在 table 级 selects
中一样,但它们的功能就好像它们是 union all
的结果上的函数.
P.S。不好意思问一个问题,当唯一的问题是我缺乏理解。
更新 table 描述:
mysql> describe checklist_10;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| foo0 | tinyint(1) | NO | | NULL | |
| foo1 | tinyint(1) | NO | | NULL | |
| foo2 | tinyint(1) | NO | | NULL | |
| foo3 | tinyint(1) | NO | | NULL | |
| foo4 | tinyint(1) | NO | | NULL | |
| foo5 | tinyint(1) | NO | | NULL | |
| foo6 | tinyint(1) | NO | | NULL | |
| foo7 | tinyint(1) | NO | | NULL | |
| foo8 | tinyint(1) | NO | | NULL | |
| foo9 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)
mysql> describe checklist_11;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| bar0 | tinyint(1) | NO | | NULL | |
| bar1 | tinyint(1) | NO | | NULL | |
| bar2 | tinyint(1) | NO | | NULL | |
| bar3 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> describe checklist_12;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| baz0 | tinyint(1) | NO | | NULL | |
| baz1 | tinyint(1) | NO | | NULL | |
| baz2 | tinyint(1) | NO | | NULL | |
| baz3 | tinyint(1) | NO | | NULL | |
| baz4 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)
数据:
mysql> select * from checklist_10;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | foo0 | foo1 | foo2 | foo3 | foo4 | foo5 | foo6 | foo7 | foo8 | foo9 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
| 1 | 1486554385343 | VRTEST | 7 | 1486556393 | 2017-02-08 12:19:53 | PERIODIC | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 2 | 1486554484635 | VRTEST | 7 | 1486556492 | 2017-02-08 12:21:32 | SHIFT | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
2 rows in set (0.00 sec)
mysql> select * from checklist_11;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | bar0 | bar1 | bar2 | bar3 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
| 1 | 1486554457077 | VRTEST | 7 | 1486556465 | 2017-02-08 12:21:05 | PERIODIC | 0 | 0 | 0 | 0 |
| 2 | 1486554490674 | VRTEST | 7 | 1486556499 | 2017-02-08 12:21:39 | SHIFT | 1 | 1 | 1 | 1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
2 rows in set (0.00 sec)
mysql> select * from checklist_12;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | baz0 | baz1 | baz2 | baz3 | baz4 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
| 1 | 1486554476903 | VRTEST | 7 | 1486556485 | 2017-02-08 12:21:25 | PERIODIC | 1 | 1 | 1 | 1 | 1 |
| 2 | 1486554496378 | VRTEST | 7 | 1486556504 | 2017-02-08 12:21:44 | SHIFT | 1 | 1 | 1 | 1 | 1 |
| 3 | 1486554496379 | VRTEST2 | 7 | 1486556505 | 2017-02-08 12:21:45 | SHIFT | 1 | 1 | 1 | 1 | 1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
3 rows in set (0.00 sec)
无 1054 错误:
mysql> select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_10
-> union all
-> select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_11
-> union all
-> select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_12
->
-> group by callsign, number
-> order by max_ts;
+--------+------------------+---------+---------------+----------+---------------------+----------+
| number | name | max(id) | max_ts | callsign | max(time_hint_utc) | count(*) |
+--------+------------------+---------+---------------+----------+---------------------+----------+
| 10 | Checklist 10 Foo | 2 | 1486554484635 | VRTEST | 2017-02-08 12:21:32 | 2 |
| 11 | Checklist 11 Bar | 2 | 1486554490674 | VRTEST | 2017-02-08 12:21:39 | 2 |
| 12 | Checklist 12 Baz | 2 | 1486554496378 | VRTEST | 2017-02-08 12:21:44 | 2 |
| 12 | Checklist 12 Baz | 3 | 1486554496379 | VRTEST2 | 2017-02-08 12:21:45 | 1 |
+--------+------------------+---------+---------------+----------+---------------------+----------+
4 rows in set (0.00 sec)
它并不像你想象的那样有效。根据documentation、
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
所以这里发生的是你有两个隐式 group by
(总是 return 一行,无论你是否在数据中有不同的呼号)和一个显式 group by
实际上只对最后一个 select.
进行分组
所以对于 MySQL,您的查询看起来和行为都像
(select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_10)
union all
(select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_11)
union all
(select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_12
group by callsign, number)
order by max_ts
你可以测试这个,例如通过更改 checklist_10
或 checklist_11
中某一行的 callsign
。它不会按预期分组。或者你可以,例如在您的第一个或最后一个查询中将别名 number
重命名为 number1
。它(仅)在第一种情况下有效,在后一种情况下,group by
现在找不到列 number
。
此外,在您的情况下,这仅在您禁用 only_full_group_by
sql 模式时才有效,因为您的 select 列表包含聚合函数以外的列。
似乎 group by
绑定到个体 selects
,但 order by
对 select 的 union all
起作用。
解决方案是在每个 select:
中放置一个 group by
子句
mysql> select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_10
-> group by callsign
->
-> union all
->
-> select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_11
-> group by callsign
->
-> union all
->
-> select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_12
-> group by callsign
->
-> order by max_ts
-> ;
+--------+------------------+---------+---------------+----------+---------------------+----------+
| number | name | max(id) | max_ts | callsign | max(time_hint_utc) | count(*) |
+--------+------------------+---------+---------------+----------+---------------------+----------+
| 10 | Checklist 10 Foo | 2 | 1486554484635 | VRTEST | 2017-02-08 12:21:32 | 2 |
| 11 | Checklist 11 Bar | 2 | 1486554490674 | VRTEST | 2017-02-08 12:21:39 | 2 |
| 12 | Checklist 12 Baz | 2 | 1486554496378 | VRTEST | 2017-02-08 12:21:44 | 2 |
| 12 | Checklist 12 Baz | 3 | 1486554496379 | VRTEST2 | 2017-02-08 12:21:45 | 1 |
| 10 | Checklist 10 Foo | 3 | 1486554496380 | VRTEST2 | 2017-02-08 12:21:46 | 1 |
+--------+------------------+---------+---------------+----------+---------------------+----------+
5 rows in set (0.00 sec)
我有一些 SQL 代码可以正常工作:
select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_10
union all
select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_11
union all
select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_12
group by callsign, number
order by max_ts
结果:
number,name,max(id),max_ts,callsign,max(time_hint_utc),count(*)
10,Checklist 10 Foo,2,1486554484635,VRTEST,2017-02-08 12:21:32,2
11,Checklist 11 Bar,2,1486554490674,VRTEST,2017-02-08 12:21:39,2
12,Checklist 12 Baz,2,1486554496378,VRTEST,2017-02-08 12:21:44,2
12,Checklist 12 Baz,3,1486554496379,VRTEST2,2017-02-08 12:21:45,1
特别是,我看到 max()
和 count()
的正确结果,即每个 callsign
/[=22 的 max
和 count
=] 组合,不是单独的 selects
.
这怎么行?聚合函数 看起来 就像它们在 table 级 selects
中一样,但它们的功能就好像它们是 union all
的结果上的函数.
P.S。不好意思问一个问题,当唯一的问题是我缺乏理解。
更新 table 描述:
mysql> describe checklist_10;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| foo0 | tinyint(1) | NO | | NULL | |
| foo1 | tinyint(1) | NO | | NULL | |
| foo2 | tinyint(1) | NO | | NULL | |
| foo3 | tinyint(1) | NO | | NULL | |
| foo4 | tinyint(1) | NO | | NULL | |
| foo5 | tinyint(1) | NO | | NULL | |
| foo6 | tinyint(1) | NO | | NULL | |
| foo7 | tinyint(1) | NO | | NULL | |
| foo8 | tinyint(1) | NO | | NULL | |
| foo9 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)
mysql> describe checklist_11;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| bar0 | tinyint(1) | NO | | NULL | |
| bar1 | tinyint(1) | NO | | NULL | |
| bar2 | tinyint(1) | NO | | NULL | |
| bar3 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> describe checklist_12;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ts | bigint(20) unsigned | NO | | NULL | |
| callsign | varchar(20) | NO | | NULL | |
| smg_id | tinyint(3) unsigned | NO | | NULL | |
| time | int(11) | NO | | NULL | |
| time_hint_utc | datetime | NO | | NULL | |
| reason | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO | | NULL | |
| baz0 | tinyint(1) | NO | | NULL | |
| baz1 | tinyint(1) | NO | | NULL | |
| baz2 | tinyint(1) | NO | | NULL | |
| baz3 | tinyint(1) | NO | | NULL | |
| baz4 | tinyint(1) | NO | | NULL | |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)
数据:
mysql> select * from checklist_10;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | foo0 | foo1 | foo2 | foo3 | foo4 | foo5 | foo6 | foo7 | foo8 | foo9 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
| 1 | 1486554385343 | VRTEST | 7 | 1486556393 | 2017-02-08 12:19:53 | PERIODIC | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 2 | 1486554484635 | VRTEST | 7 | 1486556492 | 2017-02-08 12:21:32 | SHIFT | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
2 rows in set (0.00 sec)
mysql> select * from checklist_11;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | bar0 | bar1 | bar2 | bar3 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
| 1 | 1486554457077 | VRTEST | 7 | 1486556465 | 2017-02-08 12:21:05 | PERIODIC | 0 | 0 | 0 | 0 |
| 2 | 1486554490674 | VRTEST | 7 | 1486556499 | 2017-02-08 12:21:39 | SHIFT | 1 | 1 | 1 | 1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
2 rows in set (0.00 sec)
mysql> select * from checklist_12;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
| id | ts | callsign | smg_id | time | time_hint_utc | reason | baz0 | baz1 | baz2 | baz3 | baz4 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
| 1 | 1486554476903 | VRTEST | 7 | 1486556485 | 2017-02-08 12:21:25 | PERIODIC | 1 | 1 | 1 | 1 | 1 |
| 2 | 1486554496378 | VRTEST | 7 | 1486556504 | 2017-02-08 12:21:44 | SHIFT | 1 | 1 | 1 | 1 | 1 |
| 3 | 1486554496379 | VRTEST2 | 7 | 1486556505 | 2017-02-08 12:21:45 | SHIFT | 1 | 1 | 1 | 1 | 1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
3 rows in set (0.00 sec)
无 1054 错误:
mysql> select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_10
-> union all
-> select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_11
-> union all
-> select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_12
->
-> group by callsign, number
-> order by max_ts;
+--------+------------------+---------+---------------+----------+---------------------+----------+
| number | name | max(id) | max_ts | callsign | max(time_hint_utc) | count(*) |
+--------+------------------+---------+---------------+----------+---------------------+----------+
| 10 | Checklist 10 Foo | 2 | 1486554484635 | VRTEST | 2017-02-08 12:21:32 | 2 |
| 11 | Checklist 11 Bar | 2 | 1486554490674 | VRTEST | 2017-02-08 12:21:39 | 2 |
| 12 | Checklist 12 Baz | 2 | 1486554496378 | VRTEST | 2017-02-08 12:21:44 | 2 |
| 12 | Checklist 12 Baz | 3 | 1486554496379 | VRTEST2 | 2017-02-08 12:21:45 | 1 |
+--------+------------------+---------+---------------+----------+---------------------+----------+
4 rows in set (0.00 sec)
它并不像你想象的那样有效。根据documentation、
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
所以这里发生的是你有两个隐式 group by
(总是 return 一行,无论你是否在数据中有不同的呼号)和一个显式 group by
实际上只对最后一个 select.
所以对于 MySQL,您的查询看起来和行为都像
(select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_10)
union all
(select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_11)
union all
(select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts,
callsign, max(time_hint_utc), count(*)
from checklist_12
group by callsign, number)
order by max_ts
你可以测试这个,例如通过更改 checklist_10
或 checklist_11
中某一行的 callsign
。它不会按预期分组。或者你可以,例如在您的第一个或最后一个查询中将别名 number
重命名为 number1
。它(仅)在第一种情况下有效,在后一种情况下,group by
现在找不到列 number
。
此外,在您的情况下,这仅在您禁用 only_full_group_by
sql 模式时才有效,因为您的 select 列表包含聚合函数以外的列。
似乎 group by
绑定到个体 selects
,但 order by
对 select 的 union all
起作用。
解决方案是在每个 select:
中放置一个group by
子句
mysql> select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_10
-> group by callsign
->
-> union all
->
-> select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_11
-> group by callsign
->
-> union all
->
-> select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
-> from checklist_12
-> group by callsign
->
-> order by max_ts
-> ;
+--------+------------------+---------+---------------+----------+---------------------+----------+
| number | name | max(id) | max_ts | callsign | max(time_hint_utc) | count(*) |
+--------+------------------+---------+---------------+----------+---------------------+----------+
| 10 | Checklist 10 Foo | 2 | 1486554484635 | VRTEST | 2017-02-08 12:21:32 | 2 |
| 11 | Checklist 11 Bar | 2 | 1486554490674 | VRTEST | 2017-02-08 12:21:39 | 2 |
| 12 | Checklist 12 Baz | 2 | 1486554496378 | VRTEST | 2017-02-08 12:21:44 | 2 |
| 12 | Checklist 12 Baz | 3 | 1486554496379 | VRTEST2 | 2017-02-08 12:21:45 | 1 |
| 10 | Checklist 10 Foo | 3 | 1486554496380 | VRTEST2 | 2017-02-08 12:21:46 | 1 |
+--------+------------------+---------+---------------+----------+---------------------+----------+
5 rows in set (0.00 sec)