PostgreSQL ORDER BY 语句
Posted 瀚高PG实验室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL ORDER BY 语句相关的知识,希望对你有一定的参考价值。
ORDER BY 语句
在 highgodb中,ORDER BY 用于对一列或者多列数据进行升序(ASC)或者降序(DESC)排列。
语法
ORDER BY 子句的基础语法如下:
SELECT column-list
FROM table_name[WHERE condition][ORDER BY column1, column2, .. columnN] [ASC | DESC];
您可以在 ORDER BY 中使用一列或者多列,但是必须保证要排序的列必须存在。
ASC 表示升序,DESC 表示降序。
实例
创建 class表,数据内容如下:
highgo=# select * from class;
id | name | age | birthday | address | tuition_fee
----+----------+-----+------------+-----------+-------------
1 | xiaoming | 18 | 2004-01-10 | jinan | 5000.00
2 | xiaohong | 19 | 2003-08-20 | liaocheng | 5000.00
3 | xiaoli | 18 | 2004-05-08 | taian | 6000.00
4 | xiaozhao | 19 | 2003-09-18 | jinan | 6000.00
5 | xiaowang | 18 | 2004-01-18 | jinan | 6000.00
(5 rows)
下面实例将对结果根据 AGE 字段值进行升序排列:
highgo=# select * from class ORDER BY age ASC;
得到以下结果:
id | name | age | birthday | address | tuition_fee
----+----------+-----+------------+-----------+-------------
1 | xiaoming | 18 | 2004-01-10 | jinan | 5000.00
3 | xiaoli | 18 | 2004-05-08 | taian | 6000.00
5 | xiaowang | 18 | 2004-01-18 | jinan | 6000.00
2 | xiaohong | 19 | 2003-08-20 | liaocheng | 5000.00
4 | xiaozhao | 19 | 2003-09-18 | jinan | 6000.00
(5 rows)
下面实例将对结果根据 age字段值和 tuition_fee字段值进行升序排序:
highgo=# select * from class ORDER BY age,tuition_fee ASC;
得到以下结果:
id | name | age | birthday | address | tuition_fee
----+----------+-----+------------+-----------+-------------
1 | xiaoming | 18 | 2004-01-10 | jinan | 5000.00
3 | xiaoli | 18 | 2004-05-08 | taian | 6000.00
5 | xiaowang | 18 | 2004-01-18 | jinan | 6000.00
2 | xiaohong | 19 | 2003-08-20 | liaocheng | 5000.00
4 | xiaozhao | 19 | 2003-09-18 | jinan | 6000.00
(5 rows)
下面实例将对结果根据age字段值进行降序排列:
highgo=# select * from class ORDER BY age DESC;
得到以下结果:
id | name | age | birthday | address | tuition_fee
----+----------+-----+------------+-----------+-------------
2 | xiaohong | 19 | 2003-08-20 | liaocheng | 5000.00
4 | xiaozhao | 19 | 2003-09-18 | jinan | 6000.00
1 | xiaoming | 18 | 2004-01-10 | jinan | 5000.00
3 | xiaoli | 18 | 2004-05-08 | taian | 6000.00
5 | xiaowang | 18 | 2004-01-18 | jinan | 6000.00
(5 rows)
以上是关于PostgreSQL ORDER BY 语句的主要内容,如果未能解决你的问题,请参考以下文章
PostgreSQL 可以在 ORDER BY 中使用 FOR LOOP 吗?
order by 在 POSTGRESQL 中的 partition by 子句中不起作用?