I have a table postslike this
+--------------------+--------------+
| Field | Type |
+--------------------+--------------+
| id | int(11) |
| title | varchar(255) |
| body | text |
| published_at | datetime |
+--------------------+--------------+
I want to reach orderon published_at. Normally I would do:
SELECT * FROM posts ORDER BY published_at;
But my requirement here is that the query should retrieve the results from the current date from above, and then to the previous ones, and then retrieve them from the future.
My current 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 |
+-------------------------------+----+---------------------+
While my desired result would be as follows:
+-------------------------------+----+---------------------+
| 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 |
+-------------------------------+----+---------------------+
source
share