赞
踩
在 PostgreSQL 中,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 表示降序。
实例:
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 行记录)
在 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
注意: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 行记录)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。