MySQL UNION是如何运行agregate函数的?
我有一些SQL代码,它正按照我希望的方式工作:MySQL UNION是如何运行agregate函数的?,mysql,aggregate-functions,union-all,Mysql,Aggregate Functions,Union All,我有一些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, calls
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()
的正确结果,即每个呼号的max
和count
组合,而不是单个选择的
这怎么行?聚合函数看起来像是在表级选择
,但它们的作用就像它们是联合所有
结果上的函数一样
另外,很抱歉问一个问题,唯一的问题是我缺乏理解
使用表说明更新:
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)
数据:
无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)
它不像你想象的那样工作。据报道,
如果在不包含GROUPBY子句的语句中使用group函数,则相当于对所有行进行分组
因此,这里发生的情况是,您有两个隐式的groupby
(无论您的数据中是否有不同的呼号,它总是只返回一行)和一个显式的groupby
,它实际上只对您上次选择的数据进行分组
所以对于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
您可以通过更改检查表\u 10
或检查表\u 11
中一行的呼号来测试这一点。它将不会像预期的那样分组。或者,您可以在第一次或最后一次查询中将别名number
重命名为number1
。它(仅)在第一种情况下有效,在后一种情况下,groupby
现在找不到列number
此外,在您的情况下,这仅在禁用sql模式时有效,因为您的选择列表包括聚合函数以外的列。似乎分组依据
绑定到单个选择
,但排序依据
在选择的联合所有
上运行
解决方案是在每个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)
请提供一些样本数据以及您使用样本数据和上述查询收到的输出。我很好奇,您不是在group by error中得到了1054未知列号吗?@P.Salmon MySQL允许在group by中使用别名clause@Shadow我问这个问题是因为我在尝试复制查询时遇到了错误。@P.Salmon是您的MySQL版本早于v5.7.5,MySQL中是否启用了完整的sql分组模式?我同意。OP设置测试数据的方式使其查询产生预期结果。如果将任何vtest2
数据添加到其他表中,则结果将不符合他的预期。@chrisdew上一个select语句有group by子句,其他语句则没有。group by前面的空行不能使group by应用于联合的结果。@Solarflare:order by仅应用于最后一个查询,您需要在order by的selects周围有明确的括号,才能应用于整个联合的结果集。@Shadow No,order by
与union
组合将始终作用于整个结果集,除非它明确位于括号内。两个版本(原始版本完全没有括号,另一个有括号,但外部的顺序)应该是一样的。@Solarflare我同意。引用MySQL手册:“要使用ORDER BY或LIMIT子句对整个联合结果进行排序或限制,请将各个SELECT语句括起来,并将ORDER BY或LIMIT放在最后一个语句之后。”
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)