我希望能够对ejabberd的存档表进行翻译,以对话双方为依据,整理出特定用户与每位用户之间的最后一条消息。以下是我使用的字段:
- id(消息id) - username(用户名副本) - bare_peer(正在聊天的用户) - txt(文本聊天) - created_at(创建时间)
我的目标是按照
- id(消息id) - username(用户名副本) - bare_peer(正在聊天的用户) - txt(文本聊天) - created_at(创建时间)
我的目标是按照
bare_peer
分组,其中用户名1_usernode
,只留下最后一条消息。尽管我已经尝试过多个查询语句,但都未成功。以下是我尝试的第一个查询语句。SELECT id, username, bare_peer, txt FROM archive where
username = '1_usernode' GROUP BY bare_peer ORDER BY created_at DESC;
这是输出结果。
+------+------------+-------------------------------------------------------+---------------------+
| 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 |
GROUP BY
来实现你的目标,因为GROUP BY
执行的是另一种操作。它不会返回数据库中的行,而是使用从数据库中提取的数据生成新的行。你发布的查询语句不是有效的 SQL 语句。两个(或更多)具有相同bare_peer
值的不同行将落入同一组中。它们具有不同的id
值,但你想要SELECT id
。哪个id
? - axiac