MYSQL-如何使用group by获取每个类型字段中的最新记录

时间:2018-07-12 11:54:18

标签: mysql

我有两个表格,如下所示:

CREATE TABLE `message` (
  `id` varchar(64) NOT NULL,
  `msg` varchar(2000) DEFAULT '',
  `msg_date` datetime DEFAULT NULL,
  `msg_type` int(2) DEFAULT NULL,
  `del_flag` char(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `message_receive` (
  `id` varchar(64) NOT NULL,
  `msg_id` varchar(64) NOT NULL,
  `user_id` varchar(64) DEFAULT NULL,
  `del_flag` char(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO message_receive values ('r1', 'm3', 'u1', 0);
INSERT INTO message_receive values ('r2', 'm4', 'u1', 0);
INSERT INTO message_receive values ('r3', 'm5', 'u1', 0);
INSERT INTO message_receive values ('r4', 'm6', 'u1', 0);
INSERT INTO message_receive values ('r5', 'm7', 'u1', 0);
INSERT INTO message_receive values ('r6', 'm8', 'u1', 0);
INSERT INTO message_receive values ('r7', 'm9', 'u1', 1);
INSERT INTO message_receive values ('r8', 'm10', 'u1', 0);
INSERT INTO message_receive values ('r9', 'm11', 'u1', 0);
INSERT INTO message_receive values ('r10', 'm12', 'u2', 0);
INSERT INTO message_receive values ('r11', 'm13', 'u2', 1);

INSERT INTO message values ('m1', 'msg 1', '2017-06-10 11:12:33', 3, 0);
INSERT INTO message values ('m2', 'msg 2', '2018-01-01 13:22:19', 3, 0);
INSERT INTO message values ('m3', 'msg 3', '2017-12-13 17:10:10', 3, 0);
INSERT INTO message values ('m4', 'msg 4', '2018-06-18 03:23:17', 3, 0);
INSERT INTO message values ('m5', 'msg 5', '2018-07-09 16:01:59', 3, 1);
INSERT INTO message values ('m6', 'msg 6', '2018-07-10 10:10:55', 6, 0);
INSERT INTO message values ('m7', 'msg 7', '2018-01-10 22:52:45', 6, 0);
INSERT INTO message values ('m8', 'msg 8', '2017-12-12 07:01:33', 6, 0);
INSERT INTO message values ('m9', 'msg 9', '2018-03-02 12:10:16', 6, 1);
INSERT INTO message values ('m10', 'msg 10', '2018-04-09 15:33:22', 9, 0);
INSERT INTO message values ('m11', 'msg 11', '2018-05-30 23:09:08', 9, 0);
INSERT INTO message values ('m12', 'msg 12', '2018-03-22 11:59:22', 9, 0);
INSERT INTO message values ('m13', 'msg 13', '2018-06-06 20:10:10', 9, 0);
INSERT INTO message values ('m14', 'msg 14', '2018-07-11 08:20:20', 9, 0);
INSERT INTO message values ('m15', 'msg 15', '2018-03-12 16:15:30', 9, 1);

如何从用户那里获取每个msg_type的最新记录?

如果我们想从用户u1获取记录, 正确的结果应该是:

id | msg | msg_date | msg_type
m4 | msg 4 | 2018-06-18 03:23:17 | 3
m6 | msg 6 | 2018-07-10 10:10:55 | 6
m11 | msg 11 | 2018-05-30 23:09:08 | 9

这是我所做的:

SELECT 
    t1.id, t1.msg, t1.msg_date, t1.msg_type
FROM
    message t1
        JOIN
    message_receive t2 ON t2.msg_id = t1.id
        AND t2.user_id = 'u1'
        AND t1.del_flag = '0'
        AND t2.del_flag = '0'
WHERE
    t1.msg_date IN (SELECT 
            MAX(msg_date)
        FROM
            message_receive a
                JOIN
            message b ON a.msg_id = b.id
                AND a.user_id = 'u1'
                AND a.del_flag = '0'
                AND b.del_flag = '0'
        GROUP BY msg_type)

该查询可以查询结果,但是看起来不好。有没有更好的方法来获得结果?

0 个答案:

没有答案