【发布时间】:2015-08-25 10:07:33
【问题描述】:
我有一张这样的posts 表格
+--------------------+--------------+
| Field | Type |
+--------------------+--------------+
| id | int(11) |
| title | varchar(255) |
| body | text |
| published_at | datetime |
+--------------------+--------------+
我想要实现的是order published_at。通常我会这样做:
SELECT * FROM posts ORDER BY published_at;
但我在这里的要求是查询应该从当前日期获取结果,然后是之前的结果,然后从未来获取结果。
目前我的结果如下:
+-------------------------------+----+---------------------+
| title | id | published_at |
+----------------------------------------------------------|
| Hello world | 1 | 2015-01-06 12:21:16 |
| 20+ Tools For RoR Development | 2 | 2015-08-25 12:21:23 |
| Angular JS tutorial | 3 | 2015-09-31 10:51:55 |
| Visual search | 4 | 2015-03-12 12:27:26 |
| Ruby on Rails best practices | 5 | 2015-01-21 00:00:00 |
+-------------------------------+----+---------------------+
而我想要的结果是:
+-------------------------------+----+---------------------+
| title | id | published_at |
+----------------------------------------------------------|
| 20+ Tools For RoR Development | 2 | 2015-08-25 12:21:23 |
| Hello world | 1 | 2015-01-06 12:21:16 |
| Ruby on Rails best practices | 5 | 2015-01-21 00:00:00 |
| Visual search | 4 | 2015-03-12 12:27:26 |
| Angular JS tutorial | 3 | 2015-09-31 10:51:55 |
+-------------------------------+----+---------------------+
【问题讨论】:
-
在 MongoDB 中可以吗?
-
我在 mongo 中使用聚合函数来管理这个。