当前位置:   article > 正文

PgSQL——学习笔记八: ORDER BY 子句:排序 & GROUP BY 语句:分组_pgsql order by

pgsql order by

PostgreSQL ORDER BY 语句:对一列或多列数据进行升序(ASC)或降序(DESC)排列。

在 PostgreSQL 中,ORDER BY 用于对一列或者多列数据进行升序(ASC)或者降序(DESC)排列。

//ORDER BY 子句的基础语法如下:
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
  • 1
  • 2
  • 3
  • 4
  • 5

您可以在 ORDER BY 中使用一列或者多列,但是必须保证要排序的列必须存在。
ASC 表示升序,DESC 表示降序。
实例:

mydb=# select * from COMPANY;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  3 | Teddy |  23 | Norway                                             |  20000 |
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
(5 行记录)

//将对结果根据 AGE 字段值进行升序排列:
mydb=# SELECT * FROM COMPANY ORDER BY AGE ASC;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  3 | Teddy |  23 | Norway                                             |  20000 |
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
(5 行记录)

//将对结果根据 NAME 字段值和 SALARY 字段值进行升序排序:
mydb=# SELECT * FROM COMPANY ORDER BY NAME, SALARY ASC;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  3 | Teddy |  23 | Norway                                             |  20000 |
(5 行记录)

//将对结果根据NAME字段值进行降序排列:
mydb=# SELECT * FROM COMPANY ORDER BY NAME DESC;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  3 | Teddy |  23 | Norway                                             |  20000 |
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
(5 行记录)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

PostgreSQL GROUP BY 语句:与SELECT 语句一起使用,对相同的数据进行分组

在 PostgreSQL 中,GROUP BY 语句和 SELECT 语句一起使用,用来对相同的数据进行分组。
注意:GROUP BY 在一个 SELECT 语句中,放在 WHRER 子句的后面,ORDER BY 子句的前面。

//下面给出了 GROUP BY 子句的基本语法:
SELECT column-list
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2....columnN
ORDER BY column1, column2....columnN
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意:GROUP BY 子句必须放在 WHERE 子句中的条件之后,必须放在 ORDER BY 子句之前。
在 GROUP BY 子句中,你可以对一列或者多列进行分组,但是被分组的列必须存在于列清单中。

mydb=# select * from COMPANY;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  3 | Teddy |  23 | Norway                                             |  20000 |
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
(5 行记录)

//将根据 NAME 字段值进行分组,找出每个人的工资总额:
mydb=# SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
 name  |  sum
-------+-------
 Teddy | 20000
 David | 85000
 Paul  | 20000
 Mark  | 65000
 Allen |
(5 行记录)

//在 CAMPANY 表中添加几条记录后:
mydb=# select * from COMPANY;
 id | name  | age |                      address                       | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
  4 | Mark  |  25 | Rich-Mond                                          |  65000 | 2007-12-13
  5 | David |  27 | Texas                                              |  85000 | 2007-12-13
  3 | Teddy |  23 | Norway                                             |  20000 |
  2 | Allen |  25 | Texas                                              |        | 2007-12-13
  1 | Paul  |  32 | California                                         |  20000 | 2001-07-13
  8 | Paul  |  24 | Houston                                            |  20000 |
  6 | pasl  |  24 | Hou                                                |  20000 |
  7 | lili  |  23 | hy                                                 |  10000 |
  9 | James |  44 | Norway                                             |   5000 |
 10 | James |  45 | Texas                                              |   5000 |
(10 行记录)

//根据 NAME 字段值进行分组,找出每个客户的工资总额:
mydb=# SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
 name  |  sum
-------+-------
 Teddy | 20000
 lili  | 10000
 pasl  | 20000
 David | 85000
 Paul  | 40000
 Mark  | 65000
 Allen |
 James | 10000
(8 行记录)

//将 ORDER BY 子句与 GROUP BY 子句一起使用:
mydb=# SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME ORDER BY sum ASC;
 name  |  sum
-------+-------
 lili  | 10000
 James | 10000
 Teddy | 20000
 pasl  | 20000
 Paul  | 40000
 Mark  | 65000
 David | 85000
 Allen |
(8 行记录)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/135250
推荐阅读
相关标签
  

闽ICP备14008679号