作者:Timescale 团队
PostgreSQL 或 TimescaleDB 中的 ORDER BY
子句允许您按一个或多个列以升序或降序对查询的结果集进行排序。默认情况下,排序顺序为升序。但是,如果需要,您可以指定降序。
ORDER BY
子句的基本语法如下
SELECT column1, column2, …
FROM table
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
让我们考虑一个具有以下结构和数据的表 weather_data
CREATE TABLE weather_data (
id SERIAL PRIMARY KEY,
device_id VARCHAR(50),
temperature NUMERIC,
humidity NUMERIC,
wind_speed NUMERIC,
recorded_at TIMESTAMP
);
INSERT INTO weather_data (device_id, temperature, humidity, wind_speed, recorded_at) VALUES
('device_1', 22.5, 55.0, 12.5, '2023-01-15 08:00:00'),
('device_2', 21.0, 60.0, 10.0, '2023-01-15 08:05:00'),
('device_1', 23.0, 57.0, 11.0, '2023-01-15 08:10:00'),
('device_2', 19.5, 62.0, 8.0, '2023-01-15 08:15:00'),
('device_1', 20.0, 59.0, 9.5, '2023-01-15 08:20:00'),
('device_3', 24.0, 54.0, 13.0, '2023-01-15 08:25:00'),
('device_2', 18.5, 63.0, 7.5, '2023-01-15 08:30:00'),
('device_3', 22.0, 55.5, 12.0, '2023-01-15 08:35:00'),
('device_1', 21.5, 58.0, 10.5, '2023-01-15 08:40:00'),
('device_3', 23.5, 53.0, 13.5, '2023-01-15 08:45:00');
让我们深入研究一些示例,以查看 ORDER BY
子句的实际应用。
示例 1:按单个列排序
假设我们想检索按温度升序排序的所有天气数据。我们可以使用 ORDER BY
子句来实现这一点
SELECT id, device_id, temperature, humidity, wind_speed, recorded_at
FROM weather_data
ORDER BY temperature ASC;
结果
id | device_id | temperature | humidity | wind_speed | recorded_at
----+-----------+-------------+----------+------------+---------------------
7 | device_2 | 18.5 | 63.0 | 7.5 | 2023-01-15 08:30:00
4 | device_2 | 19.5 | 62.0 | 8.0 | 2023-01-15 08:15:00
5 | device_1 | 20.0 | 59.0 | 9.5 | 2023-01-15 08:20:00
2 | device_2 | 21.0 | 60.0 | 10.0 | 2023-01-15 08:05:00
9 | device_1 | 21.5 | 58.0 | 10.5 | 2023-01-15 08:40:00
1 | device_1 | 22.5 | 55.0 | 12.5 | 2023-01-15 08:00:00
8 | device_3 | 22.0 | 55.5 | 12.0 | 2023-01-15 08:35:00
3 | device_1 | 23.0 | 57.0 | 11.0 | 2023-01-15 08:10:00
10 | device_3 | 23.5 | 53.0 | 13.5 | 2023-01-15 08:45:00
6 | device_3 | 24.0 | 54.0 | 13.0 | 2023-01-15 08:25:00
此查询按 temperature
列升序对行进行排序。
示例 2:按多列排序
现在,假设我们想先按 device_id
然后按 recorded_at
升序对天气数据进行排序。我们可以将 ORDER BY
子句与多列一起使用
SELECT id, device_id, temperature, humidity, wind_speed, recorded_at
FROM weather_data
ORDER BY device_id ASC, recorded_at ASC;
结果
id | device_id | temperature | humidity | wind_speed | recorded_at
----+-----------+-------------+----------+------------+---------------------
1 | device_1 | 22.5 | 55.0 | 12.5 | 2023-01-15 08:00:00
3 | device_1 | 23.0 | 57.0 | 11.0 | 2023-01-15 08:10:00
5 | device_1 | 20.0 | 59.0 | 9.5 | 2023-01-15 08:20:00
9 | device_1 | 21.5 | 58.0 | 10.5 | 2023-01-15 08:40:00
2 | device_2 | 21.0 | 60.0 | 10.0 | 2023-01-15 08:05:00
4 | device_2 | 19.5 | 62.0 | 8.0 | 2023-01-15 08:15:00
7 | device_2 | 18.5 | 63.0 | 7.5 | 2023-01-15 08:30:00
6 | device_3 | 24.0 | 54.0 | 13.0 | 2023-01-15 08:25:00
8 | device_3 | 22.0 | 55.5 | 12.0 | 2023-01-15 08:35:00
10 | device_3 | 23.5 | 53.0 | 13.5 | 2023-01-15 08:45:00
此查询先按 device_id
然后按 recorded_at
升序对行进行排序。
示例 3:按降序排序
假设我们想检索按 humidity
降序排序的天气数据。我们可以将 ORDER BY
子句与 DESC
一起使用
SELECT id, device_id, temperature, humidity, wind_speed, recorded_at
FROM weather_data
ORDER BY humidity DESC;
结果
id | device_id | temperature | humidity | wind_speed | recorded_at
----+-----------+-------------+----------+------------+---------------------
7 | device_2 | 18.5 | 63.0 | 7.5 | 2023-01-15 08:30:00
4 | device_2 | 19.5 | 62.0 | 8.0 | 2023-01-15 08:15:00
2 | device_2 | 21.0 | 60.0 | 10.0 | 2023-01-15 08:05:00
5 | device_1 | 20.0 | 59.0 | 9.5 | 2023-01-15 08:20:00
9 | device_1 | 21.5 | 58.0 | 10.5 | 2023-01-15 08:40:00
3 | device_1 | 23.0 | 57.0 | 11.0 | 2023-01-15 08:10:00
8 | device_3 | 22.0 | 55.5 | 12.0 | 2023-01-15 08:35:00
1 | device_1 | 22.5 | 55.0 | 12.5 | 2023-01-15 08:00:00
6 | device_3 | 24.0 | 54.0 | 13.0 | 2023-01-15 08:25:00
10 | device_3 | 23.5 | 53.0 | 13.5 | 2023-01-15 08:45:00
此查询按 humidity
列降序对行进行排序。
示例 4:将 ORDER BY 与 LIMIT 结合使用
我们还可以将 ORDER BY
与 LIMIT
结合使用,以检索特定数量的已排序行。例如,要获取记录的前三个最高风速
SELECT id, device_id, temperature, humidity, wind_speed, recorded_at
FROM weather_data
ORDER BY wind_speed DESC
LIMIT 3;
结果
id | device_id | temperature | humidity | wind_speed | recorded_at
----+-----------+-------------+----------+------------+---------------------
10 | device_3 | 23.5 | 53.0 | 13.5 | 2023-01-15 08:45:00
6 | device_3 | 24.0 | 54.0 | 13.0 | 2023-01-15 08:25:00
1 | device_1 | 22.5 | 55.0 | 12.5 | 2023-01-15 08:00:00
wind_speed
降序对行进行排序,并返回前三行。ORDER BY
子句是 SQL 和 PostgreSQL 的基本组成部分,它允许您根据指定的列对查询结果进行排序。要了解更多关于 ORDER BY
的信息,请查看 PostgreSQL 文档。如果您想了解更多关于其他 PostgreSQL 子句和基础知识的信息,请访问我们的 Postgres 基础知识部分。