SQL中的'GROUP BY'和'ORDER BY'有什么区别?

What is the difference between 'GROUP BY' and 'ORDER BY' in SQL?

查询:

select name, count(*) as num from animals
group by name
order by num desc
limit 5;

当我用 group 替换 order 时,出现语法错误。

请用详细的例子回答以获得更多帮助。

不,你不能改变 SQL 子句的顺序,它应该是下面的形式:

SELECT <attribute and function list>
FROM <table list>
[ WHERE <condition> ]
[ GROUP BY <grouping attribute(s)> ]
[ HAVING <group condition> ]
[ ORDER BY <attribute list> ];

SQL 中的检索查询由这(最多)六个子句组成,但只有前两个 - SELECTFROM - 是强制性的。

GROUP BY 指定分组属性(不保证结果出现的顺序),而 ORDER BY 指定显示查询结果的顺序。

:

QUERY = "select * from animals where species = 'orangutan' order by name ;" here when i replace order with group i didn't saw any difference

标准 SQL 不保证结果出现的顺序,包含 GROUP BY 子句的查询 不能引用 select 列表中的非聚合列未在 GROUP BY 子句中命名的。

所以:

select * 
from animals 
where species = 'orangutan' 
group by name;

查询在标准SQL中是非法的,因为它包括table的每个字段。检查此 MySQL 文档 MySQL Handling of GROUP BY.

然而,

select * 
from animals 
where species = 'orangutan' 
order by name;

完全有效。

无论如何,一些SQL的实现如MySQL也会对输出结果进行排序,检查:

12.17.3 MySQL Handling of GROUP BY

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However.....

GROUP BY 如何排序?

Other Considerations When using ROLLUP

GROUP BY in MySQL sorts results, and you can use explicit ASC and DESC keywords with columns named in the GROUP BY list to specify sort order for individual columns.

类似的,在您的 DBMS 中也发现了行为。

如果你可以标记你的 DBMS 的名称,那么我或其他人可以通过一些例子来模拟它们之间的区别。下面我在 MySQL:

中给出一个例子

12.17.3 MySQL Handling of GROUP BY

.... However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.

我有一个员工 table,如下所示:

mysql> select * FROM Employee;
+-----+------+-------------+------+
| SSN | Name | Designation | MSSN |
+-----+------+-------------+------+
| 1   | A    | OWNER       | 1    |
| 10  | G    | WORKER      | 5    |
| 11  | D    | WORKER      | 4    |
| 12  | E    | WORKER      | 4    |
| 2   | B    | BOSS        | 1    |
| 3   | F    | BOSS        | 1    |
| 4   | C    | BOSS        | 2    |
| 5   | H    | BOSS        | 2    |
| 6   | L    | WORKER      | 2    |
| 7   | I    | BOSS        | 2    |
| 8   | K    | WORKER      | 3    |
| 9   | J    | WORKER      | 7    |
+-----+------+-------------+------+
12 rows in set (0.00 sec)

如果我按 'Designation' 应用分组,它会生成分组并排序结果

mysql> select * from Employee group by Designation;
+-----+------+-------------+------+
| SSN | Name | Designation | MSSN |
+-----+------+-------------+------+
| 2   | B    | BOSS        | 1    |
| 1   | A    | OWNER       | 1    |
| 10  | G    | WORKER      | 5    |
+-----+------+-------------+------+
3 rows in set (0.00 sec)

table 中存在的三种类型 'Designation' 值的三行,根据 MySQL 文档

,选择哪一个是不确定的

和order by query的结果完全不同:

mysql> select * from Employee order by Designation;
+-----+------+-------------+------+
| SSN | Name | Designation | MSSN |
+-----+------+-------------+------+
| 4   | C    | BOSS        | 2    |
| 7   | I    | BOSS        | 2    |
| 5   | H    | BOSS        | 2    |
| 2   | B    | BOSS        | 1    |
| 3   | F    | BOSS        | 1    |
| 1   | A    | OWNER       | 1    |
| 12  | E    | WORKER      | 4    |
| 11  | D    | WORKER      | 4    |
| 6   | L    | WORKER      | 2    |
| 10  | G    | WORKER      | 5    |
| 8   | K    | WORKER      | 3    |
| 9   | J    | WORKER      | 7    |
+-----+------+-------------+------+
12 rows in set (0.00 sec)