我理解GROUP BY x
但GROUP BY x, y
如何运作,它的含义是什么?
答案 0 :(得分:1798)
Group By X
表示将所有具有相同X值的组合放入一个组。
Group By X, Y
表示将所有具有相同值的值放在一个组中的X和Y 。
为了说明一个例子,假设我们有下表,与谁在大学的哪个科目上学习有关:
Table: Subject_Selection
Subject Semester Attendee
---------------------------------
ITB001 1 John
ITB001 1 Bob
ITB001 1 Mickey
ITB001 2 Jenny
ITB001 2 James
MKB114 1 John
MKB114 1 Erica
仅在主题列上使用group by
时;说:
select Subject, Count(*)
from Subject_Selection
group by Subject
你会得到类似的东西:
Subject Count
------------------------------
ITB001 5
MKB114 2
...因为ITB001有5个条目,MKB114有2个
如果我们要group by
两列:
select Subject, Semester, Count(*)
from Subject_Selection
group by Subject, Semester
我们会得到这个:
Subject Semester Count
------------------------------
ITB001 1 3
ITB001 2 2
MKB114 1 2
这是因为,当我们按两列分组时,它会说“对它们进行分组,以便所有具有相同主题和学期的人都在同一个组中,然后计算所有聚合函数(计数,总和,平均等)对于每个组“。在这个例子中,事实证明了这一点,当我们计算它们时,三个的人在第一学期做ITB001,而两个在第二学期做这个。做MKB114的人在第一学期,因此第二学期没有行(没有数据适合“MKB114,第二学期”)
希望这是有道理的。
答案 1 :(得分:21)
GROUP BY
子句与聚合函数结合使用,以按一列或多列对结果集进行分组。 e.g:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
记住这个顺序:
1)SELECT(用于从数据库中选择数据)
2)FROM(子句用于列出表格)
3)WHERE(子句用于过滤记录)
4)GROUP BY(子句可以在SELECT语句中用于收集数据 跨多个记录并按一个或多个列对结果进行分组)
5)HAVING(子句与GROUP BY子句结合使用 将返回行的组限制为仅包含其条件的行 是真的)
6)ORDER BY(关键字用于对结果集进行排序)
如果使用聚合函数,则可以使用所有这些,这是必须设置的顺序,否则可能会出错。
聚合函数是:
MIN返回给定列中的最小值
SUM返回给定列中数值的总和
AVG返回给定列的平均值
COUNT返回给定列中的值总数
COUNT(*)返回表中的行数
答案 2 :(得分:1)
在带有两个参数的 GROUP BY
中的简单英语中,我们正在寻找相似的值对并将计数添加到第 3 列。
请看以下示例以供参考。我在这里使用 International football results from 1872 to 2020
+----------+----------------+--------+---+---+--------+---------+-------------------+-----+
| _c0| _c1| _c2|_c3|_c4| _c5| _c6| _c7| _c8|
+----------+----------------+--------+---+---+--------+---------+-------------------+-----+
|1872-11-30| Scotland| England| 0| 0|Friendly| Glasgow| Scotland|FALSE|
|1873-03-08| England|Scotland| 4| 2|Friendly| London| England|FALSE|
|1874-03-07| Scotland| England| 2| 1|Friendly| Glasgow| Scotland|FALSE|
|1875-03-06| England|Scotland| 2| 2|Friendly| London| England|FALSE|
|1876-03-04| Scotland| England| 3| 0|Friendly| Glasgow| Scotland|FALSE|
|1876-03-25| Scotland| Wales| 4| 0|Friendly| Glasgow| Scotland|FALSE|
|1877-03-03| England|Scotland| 1| 3|Friendly| London| England|FALSE|
|1877-03-05| Wales|Scotland| 0| 2|Friendly| Wrexham| Wales|FALSE|
|1878-03-02| Scotland| England| 7| 2|Friendly| Glasgow| Scotland|FALSE|
|1878-03-23| Scotland| Wales| 9| 0|Friendly| Glasgow| Scotland|FALSE|
|1879-01-18| England| Wales| 2| 1|Friendly| London| England|FALSE|
|1879-04-05| England|Scotland| 5| 4|Friendly| London| England|FALSE|
|1879-04-07| Wales|Scotland| 0| 3|Friendly| Wrexham| Wales|FALSE|
|1880-03-13| Scotland| England| 5| 4|Friendly| Glasgow| Scotland|FALSE|
|1880-03-15| Wales| England| 2| 3|Friendly| Wrexham| Wales|FALSE|
|1880-03-27| Scotland| Wales| 5| 1|Friendly| Glasgow| Scotland|FALSE|
|1881-02-26| England| Wales| 0| 1|Friendly|Blackburn| England|FALSE|
|1881-03-12| England|Scotland| 1| 6|Friendly| London| England|FALSE|
|1881-03-14| Wales|Scotland| 1| 5|Friendly| Wrexham| Wales|FALSE|
|1882-02-18|Northern Ireland| England| 0| 13|Friendly| Belfast|Republic of Ireland|FALSE|
+----------+----------------+--------+---+---+--------+---------+-------------------+-----+
现在我将通过 _c7
操作按相似国家(列 _c5
)和锦标赛(GROUP BY
)值对分组,
SELECT `_c5`,`_c7`,count(*) FROM res GROUP BY `_c5`,`_c7`
+--------------------+-------------------+--------+
| _c5| _c7|count(1)|
+--------------------+-------------------+--------+
| Friendly| Southern Rhodesia| 11|
| Friendly| Ecuador| 68|
|African Cup of Na...| Ethiopia| 41|
|Gold Cup qualific...|Trinidad and Tobago| 9|
|AFC Asian Cup qua...| Bhutan| 7|
|African Nations C...| Gabon| 2|
| Friendly| China PR| 170|
|FIFA World Cup qu...| Israel| 59|
|FIFA World Cup qu...| Japan| 61|
|UEFA Euro qualifi...| Romania| 62|
|AFC Asian Cup qua...| Macau| 9|
| Friendly| South Sudan| 1|
|CONCACAF Nations ...| Suriname| 3|
| Copa Newton| Argentina| 12|
| Friendly| Philippines| 38|
|FIFA World Cup qu...| Chile| 68|
|African Cup of Na...| Madagascar| 29|
|FIFA World Cup qu...| Burkina Faso| 30|
| UEFA Nations League| Denmark| 4|
| Atlantic Cup| Paraguay| 2|
+--------------------+-------------------+--------+
说明:第一行的意思是南罗得西亚一共举办了11场友谊赛。
注意:在这种情况下,必须使用计数器列。