获取每组的最新记录

时间:2016-09-27 12:47:17

标签: mysql sorting group-by

我想对特定用户从ejabberd归档表中查找的每个用户对最后一条消息进行排序。

我正在使用的字段是

  • 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 |

7 个答案:

答案 0 :(得分:0)

尝试此查询: -

SELECT archive.id, archive.max_id, archive.username, archive.bare_peer, archive.txt 
FROM archive join 
(SELECT MAX(id) max_id, username, bare_peer, txt 
FROM archivewhere username = '1_usernode' GROUP BY bare_peer)
tab on archive.id=tab.max_id

答案 1 :(得分:0)

使用此查询很有帮助。

SELECT MAX(id), username, bare_peer, txt FROM archive where
username = '1_usernode' ORDER BY created_at DESC

答案 2 :(得分:0)

将created_at声明为datetime

试试这个

DROP TABLE IF EXISTS `archive`;

CREATE TABLE `archive` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) DEFAULT NULL,
  `bare_peer` VARCHAR(50) DEFAULT NULL,
  `txt` TEXT,
  `created_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB  DEFAULT CHARSET=latin1;

/ *表archive * /

的数据
INSERT  INTO `archive`(`id`,`username`,`bare_peer`,`txt`,`created_at`) 
VALUES (1034,'1_usernode','15_usernode@localhost','hey sup ?','2016-07-13 22:40:29'),
(1078,'1_usernode','2_usernode@localhost','Hello this IS just a Test!!!','2016-07-15 16:30:50'),
(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'),
(1095,'1_usernode','10_usernode@localhost','Hello !!!','2016-07-17 21:15:17'),
(1101,'1_usernode','7_usernode@localhost','hey','2016-07-18 00:05:55'),
(1107,'1_usernode','13_usernode@localhost','oi','2016-07-18 00:09:28');

然后运行您的查询

SELECT id, username, bare_peer, txt FROM archive where
username = '1_usernode' GROUP BY bare_peer ORDER BY created_at DESC;

答案 3 :(得分:0)

请尝试以下代码: -

select m.*
from
messages m
inner join (
select max(id) as maxid
from messages
group By (if(username > bare_peer,  username, bare_peer)), 
(if(username > bare_peer,  bare_peer, username))
) t1 on m.id=t1.maxid ;

m是消息表的别名

答案 4 :(得分:0)

您希望每个用户名和bare_peer都使用max(created_at)条目。 在MySQL中实现这一点的一种方法是“拥有”,但我不喜欢这样。 我首先得到每个条目的max(created_at):

select username, bare_peer, max(created_at) as m_
from archive
group by username, bare_peer;

然后加入关于该结果的表格:

select b.*
from (
    select username, bare_peer, max(created_at) as m_
    from archive
    group by username, bare_peer
) a
inner join archive as b on (
    a.username = b.username
    and a.bare_peer = b.bare_peer
    and a.m_ = b.created_at
)

答案 5 :(得分:0)

我想知道为什么当你不选择created_at时它会显示created_at列?而且我不知道您使用group by的原因?没有必要分成几组。

我的陈述是这样的。select id, username, bare_peer, txt, created_at from archive where username = '1_usercode' order by created_at desc

答案 6 :(得分:0)

我用Rahauto的答案创建了一个临时解决方案。 我把他的查询从我最新的消息中返回给我正确的id 子查询,以便我可以从它的id中提取消息内容。

SELECT username, bare_peer, txt, created_at FROM archive WHERE id IN (
  SELECT tab.max_id  FROM
    archive JOIN (SELECT MAX(id) max_id, username, bare_peer, txt FROM
    archive WHERE username = '1_usernode' GROUP BY bare_peer)
  tab ON archive.id=tab.max_id
);
相关问题