I have a table of posts
like this
+--------------------+--------------+
| Field | Type |
+--------------------+--------------+
| id | int(11) |
| title | varchar(255) |
| body | text |
| published_at | datetime |
+--------------------+--------------+
What I want achieve is to order
by published_at
. Normally I would do:
SELECT * FROM posts ORDER BY published_at;
But my requirement here is that the query should fetch the results from current date on top and then the previous ones and after that fetch those from future.
Current my results are as follows:
+-------------------------------+----+---------------------+
| 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 |
+-------------------------------+----+---------------------+
Whereas my desired outcome would be:
+-------------------------------+----+---------------------+
| 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 |
+-------------------------------+----+---------------------+