I want to sort the last messages with every user that a specific user has chated from ejabberd archive table.
The fields that I'm using are these
id
(message id)username
(username copy)bare_peer
(user that is chatting with)txt
(text chat)created_at
(time created)
What I'm trying to achieve is something like that, but I need to group message by bare_peer
with username as 1_usernode
, but only the last messages.
I already tested a lot of queries, but none of them worked. This is the first query I tried.
SELECT id, username, bare_peer, txt FROM archive where
username = '1_usernode' GROUP BY bare_peer ORDER BY created_at DESC;
And this is the output.
+------+------------+-------------------------------------------------------+---------------------+
| id | username | bare_peer | txt | created_at |
+------+------------+------------------------+------------------------------+---------------------+
| 1095 | 1_usernode | 10_usernode@localhost | Hello !!! | 2016-07-17 21:15:17 |
| 1034 | 1_usernode | 15_usernode@localhost | hey sup ? | 2016-07-13 22:40:29 |
| 1107 | 1_usernode | 13_usernode@localhost | oi | 2016-07-18 00:09:28 |
| 1078 | 1_usernode | 2_usernode@localhost | Hello this is just a Test!!! | 2016-07-15 16:30:50 |
| 1101 | 1_usernode | 7_usernode@localhost | hey | 2016-07-18 00:05:55 |
| 1084 | 1_usernode | 3_usernode@localhost | Hey how are you? | 2016-07-15 19:36:44 |
| 1085 | 1_usernode | 4_usernode@localhost | Hey how are you doing ? | 2016-07-17 19:20:00 |