PostgreSQL ORDER BY 語句

2020-04-22 17:53 更新

在 PostgreSQL 中,ORDER BY 用于對一列或者多列數(shù)據(jù)進(jìn)行升序(ASC)或者降序(DESC)排列。

語法

ORDER BY 子句的基礎(chǔ)語法如下:

SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];

您可以在 ORDER BY 中使用一列或者多列,但是必須保證要排序的列必須存在。

ASC 表示升序,DESC 表示降序。

實(shí)例

創(chuàng)建 COMPANY 表 ,數(shù)據(jù)內(nèi)容如下:

w3cschooldb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

下面實(shí)例將對結(jié)果根據(jù) AGE 字段值進(jìn)行升序排列:

w3cschooldb=# SELECT * FROM COMPANY ORDER BY AGE ASC;

得到以下結(jié)果:

 id | name  | age |                      address                       | salary 
----+-------+-----+----------------------------------------------------+--------
  6 | Kim   |  22 | South-Hall                                         |  45000
  3 | Teddy |  23 | Norway                                             |  20000
  7 | James |  24 | Houston                                            |  10000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  2 | Allen |  25 | Texas                                              |  15000
  5 | David |  27 | Texas                                              |  85000
  1 | Paul  |  32 | California                                         |  20000
(7 rows)

下面實(shí)例將對結(jié)果根據(jù) NAME 字段值和 SALARY 字段值進(jìn)行升序排序:

w3cschooldb=# SELECT * FROM COMPANY ORDER BY NAME, SALARY ASC;

得到以下結(jié)果:

 id | name  | age |                      address                       | salary 
----+-------+-----+----------------------------------------------------+--------
  2 | Allen |  25 | Texas                                              |  15000
  5 | David |  27 | Texas                                              |  85000
  7 | James |  24 | Houston                                            |  10000
  6 | Kim   |  22 | South-Hall                                         |  45000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  1 | Paul  |  32 | California                                         |  20000
  3 | Teddy |  23 | Norway                                             |  20000
(7 rows)

下面實(shí)例將對結(jié)果根據(jù)NAME字段值進(jìn)行降序排列:

w3cschooldb=# SELECT * FROM COMPANY ORDER BY NAME DESC;

得到以下結(jié)果:

 id | name  | age |                      address                       | salary 
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  20000
  1 | Paul  |  32 | California                                         |  20000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
  5 | David |  27 | Texas                                              |  85000
  2 | Allen |  25 | Texas                                              |  15000
(7 rows)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號