I have a normal comments table:
| id | comment | date | user | post | status |
I want to fetch my 10 last comments with just 1 comment per user, I mean something like this:
I have this data:
| id | comment | date | user | post |
| 1 | text1 | 2016-04-01| 1 | 1 |
| 2 | text2 | 2016-04-02| 2 | 1 |
| 3 | text3 | 2016-04-03| 1 | 2 |
| 4 | text4 | 2016-04-04| 4 | 3 |
| 5 | text5 | 2016-04-04| 3 | 5 |
| 6 | text6 | 2016-04-05| 2 | 4 |
| 7 | text7 | 2016-04-07| 5 | 3 |
| 8 | text8 | 2016-04-10| 4 | 9 |
| 9 | text9 | 2016-04-11| 3 | 7 |
| 10 | text10 | 2016-04-12| 5 | 8 |
and I want get this :
| 10 | text10 | 2016-04-12| 5 | 8 |
| 9 | text9 | 2016-04-11| 3 | 7 |
| 8 | text8 | 2016-04-10| 4 | 9 |
| 6 | text6 | 2016-04-05| 2 | 4 |
| 3 | text3 | 2016-04-03| 1 | 2 |
I use this sql statement:
SELECT *
FROM `comments`
GROUP BY `user`
ORDER BY MAX(`id`) DESC
This fetches comments with correct order for user but wrong order for comment's date:
| 7 | text7 | 2016-04-07| 5 | 3 |
| 9 | text9 | 2016-04-11| 3 | 7 |
| 4 | text4 | 2016-04-04| 4 | 3 |
| 6 | text6 | 2016-04-05| 2 | 4 |
| 1 | text1 | 2016-04-01| 1 | 1 |