Postgresql,排序选定的数据(时间戳)

Postgresql, sorting selected data(timestamp)

假设我从两个表中完成了 select:

select a.frequency+b.lastrun
from a, b where a.id = b.a_id;

我得到输出:

          ?column?
----------------------------
 2015-01-03 09:02:10.300201
 2015-01-09 09:02:10.300201
 2015-01-03 09:02:10.300201
 2015-01-02 21:02:10.300201
 2015-01-02 15:02:10.300201
 2015-01-09 09:02:10.300201
 2015-02-01 09:02:10.300201
 2015-01-03 09:02:10.300201
 2015-01-02 10:02:10.300201
 2015-01-03 09:02:10.300201

如何按递减对这些数据进行排序?

您可以简单地为 order by 子句提供列号:

select a.frequency+b.lastrun
from a, b 
where a.id = b.a_id
order by 1;

或使用别名

select a.frequency+b.lastrun as ts
from a, b 
where a.id = b.a_id
order by ts;

这在手册中有详细记录:
http://www.postgresql.org/docs/current/static/queries-order.html


您还应该习惯于显式连接,而不是旧式的隐式连接。

select a.frequency+b.lastrun
from a
  join b on a.id = b.a_id
order by 1;