I have 2 tables that involve this query.
Table : Discussions Table : Comments
-------------------- -------------------------------------
|id | content | | id | d_id | content | timestamp |
-------------------- -------------------------------------
| 1 | Text String | | 1 | 2 | Comment 1 | timestamp |
| 2 | Text String | | 2 | 3 | Comment 2 | timestamp |
| 3 | Text String | | 3 | 4 | Comment 3 | timestamp |
| 4 | Text String | | 4 | 2 | Comment 4 | timestamp |
| 5 | Text String | | 5 | 3 | Comment 5 | timestamp |
-------------------- -------------------------------------
Now I need to get the id
from the discussion
table, and I need to sort it using the d_id
in the comment
table according to a Unix timestamp in the comments table. If there is no comment then I need to sort discussions using timestamp in the discussions table. so newly posted discussions without any comments will added to the top of the page.
JOIN
and anORDER BY
clause in the query you tried, right? – mustaccio Mar 2 '17 at 16:11