I have the following table
ID | tbl | reply | time
--- ----- ------- -----------
1 | blue| 0 | 01:00
--- ----- ------- -----------
2 | red | 1 | 01:05
--- ----- ------- -----------
3 | pink| 1 | 11:11
--- ----- ------- -----------
4 | pink| 0 | 22:22
--- ----- ------- -----------
5 | pink| 4 | 00:03
--- ----- ------- -----------
The reply refers to the ID column so ID and reply have a connection. ID is the parent of reply. When reply=0 then it means that we have selected a parent row else it is just the reply.
At the moment I have this simple mysql query in order to select only the parents
SELECT tbl, time FROM table WHERE reply=0 ORDER BY time ASC
Which will select first the ID=1 and then the ID=4
What I want is to be able to ORDER the parents by the time of the childrens but not to show the childrens. So in the above example just because ID=5 has a time of 0:03 then the ID=4 should appear first and then ID=1
Any ideas how to do this except from creating another table?
If it is better maybe any php ideas on how to do this?
Thank you very much
My SQL is a bit rusty, but:
The trick is to give the main query table a name (
tin this example) – you can then reference it in the subquery without the server getting confused.I know this might just be your example, but you’d be best storing your time column as a DATETIME or even a UNIX timestamp (in an INT field). You’ll need the date, so that something posted yesterday at 23:00 isn’t seen as more recent than something posted today at 15:00