我正在尝试获取最新消息列表。情况就是这样:当有完全相同的时间发送消息时,续集声明会选择所有消息。每个聊天室我只需要一条消息,最新消息。在@JuanCarlosOropeza的帮助下(以及我将在这个问题上提出的每一个问题中都会包含他的精湛小提琴http://sqlfiddle.com/#!9/d506e/10)我得到了下面的续集声明。
SELECT *
FROM ( SELECT t.*,
@rn := if(@friend = t.friend_id,
@rn + 1,
if( @friend := t.friend_id, 1, 1)
) as rn
FROM (
SELECT m.message,
m.message_read,
m.message_date,
CASE WHEN m.sender = 4
THEN m.receiver
ELSE m.sender
END as friend_id,
CASE WHEN m.sender = 4
THEN p2.nickname
ELSE p1.nickname
END as name,
CASE WHEN m.sender = 4
THEN p2.image
ELSE p1.image
END as image
FROM message as m
JOIN profile as p1
ON m.sender = p1.user_id -- sender
JOIN profile as p2
ON m.receiver = p2.user_id -- receiver
WHERE 4 IN (m.sender, m.receiver)
) as t
CROSS JOIN ( SELECT @rn := 0, @friend := 0 ) as var
ORDER BY t.friend_id, t.message_date desc
) q
WHERE q.rn = 1
上述续集声明做得很好;但是,它有一个缺陷。 ORDER BY t.friend_id, t.message_date desc
未考虑“完全相同的message_date”。很可能不可能同时发送消息,但谁知道呢?
朋友表:
+----+-------+-------+---------+--------------+
ø id ø user1 ø user2 ø pending ø request_date ø
+----+-------+-------+---------+--------------+
ø 1 ø 4 ø 2 ø 0 ø 2018-05-09 ø
ø 2 ø 5 ø 2 ø 0 ø 2018-05-09 ø
ø 3 ø 1 ø 4 ø 0 ø 2018-05-09 ø
+----+-------+-------+---------+--------------+
个人资料表:
+----+---------+----------------------+---------------+-------+
ø id ø user_id ø nickname ø email ø image ø
+----+---------+----------------------+---------------+-------+
ø 1 ø 1 ø Welcome to MathMatch ø test@test.com ø NULL ø
ø 2 ø 2 ø user3 ø NULL ø NULL ø
ø 3 ø 3 ø USER4 ø NULL ø NULL ø
ø 4 ø 4 ø elokiller ø NULL ø NULL ø
ø 5 ø 5 ø USER6 ø NULL ø NULL ø
+----+---------+----------------------+---------------+-------+
消息表:
+----+--------+----------+-----------+--------------+-----------------+---------------------+
ø id ø sender ø receiver ø message ø message_read ø message_visible ø message_date ø
+----+--------+----------+-----------+--------------+-----------------+---------------------+
ø 1 ø 4 ø 2 ø lulz ø 1 ø 2 ø 2018-05-15 10:24:36 ø
ø 2 ø 1 ø 4 ø Hey Buddy ø 1 ø NULL ø 2018-05-10 11:58:39 ø
ø 3 ø 2 ø 4 ø nooo ø 1 ø 2 ø 2018-05-15 10:24:36 ø
ø 4 ø 4 ø 2 ø shut up ø 1 ø 2 ø 2018-05-15 10:24:36 ø
+----+--------+----------+-----------+--------------+-----------------+---------------------+
如果您知道如何解决此问题,那就太棒了。我在运行上述续集声明的这一刻得到的结果如下。
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
ø message ø message_read ø message_date ø friend_id ø name ø image ø rn ø
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
ø Hey Buddy ø 1 ø 2018-05-10 11:58:39 ø 1 ø Welcome to MathMatch ø NULL ø 1 ø
ø lulz ø 1 ø 2018-05-15 10:24:36 ø 2 ø user3 ø NULL ø 1 ø
ø nooo ø 1 ø 2018-05-15 10:24:36 ø 2 ø user3 ø NULL ø 1 ø
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
结果我期待:
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
ø message ø message_read ø message_date ø friend_id ø name ø image ø rn ø
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
ø Hey Buddy ø 1 ø 2018-05-10 11:58:39 ø 1 ø Welcome to MathMatch ø NULL ø 1 ø
ø lulz ø 1 ø 2018-05-15 10:24:36 ø 2 ø user3 ø NULL ø 1 ø
+-----------+--------------+---------------------+-----------+----------------------+-------+------+
如您所见,来自同一个friend_id的消息有两条,即2.我只想从该朋友中检索一条消息。感谢您抽出宝贵时间。任何评论都会有所帮助〜
另一个值得关注的场景。
当前消息表:
+----+--------+----------+-----------+--------------+-----------------+---------------------+
| id | sender | receiver | message | message_read | message_visible | message_date |
+----+--------+----------+-----------+--------------+-----------------+---------------------+
| 1 | 4 | 2 | lulz | 1 | 2 | 2018-05-15 10:24:36 |
| 2 | 1 | 4 | Hey Buddy | 1 | NULL | 2018-05-10 11:58:39 |
| 3 | 2 | 4 | nooo | 1 | 2 | 2018-05-15 10:24:36 |
| 4 | 4 | 2 | shut up | 1 | 2 | 2018-05-15 10:24:36 |
| 5 | 4 | 2 | heha | 1 | NULL | 2018-05-15 10:36:11 |
| 6 | 1 | 4 | SUP MATE | 1 | NULL | 2018-05-15 11:04:24 |
+----+--------+----------+-----------+--------------+-----------------+---------------------+
当前结果表:
+-----------+--------------+---------------------+-----------+-------+-------+------+
| message | message_read | message_date | friend_id | name | image | rn |
+-----------+--------------+---------------------+-----------+-------+-------+------+
| Hey Buddy | 1 | 2018-05-10 11:58:39 | 1 | JUAN | NULL | 1 |
| nooo | 1 | 2018-05-15 10:24:36 | 2 | user3 | NULL | 1 |
+-----------+--------------+---------------------+-----------+-------+-------+------+
期待结果
+-----------+--------------+---------------------+-----------+-------+-------+------+
| message | message_read | message_date | friend_id | name | image | rn |
+-----------+--------------+---------------------+-----------+-------+-------+------+
| SUP MATE | 1 | 2018-05-15 11:04:24 | 1 | JUAN | NULL | 1 |
| heha | 1 | 2018-05-15 10:36:11 | 2 | user3 | NULL | 1 |
+-----------+--------------+---------------------+-----------+-------+-------+------+
答案 0 :(得分:0)
如果您在结果表中添加了ID,那么您可以通过friend_id选择最新的ID和组。
假设您能够构建以下各种类型的视图
CREATE TABLE IF NOT EXISTS `MESSAGES` (
`id` int(6) unsigned NOT NULL,
`friend_id` int(3) unsigned NOT NULL,
`message_time` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `MESSAGES` (`id`, `friend_id`, `message_time`) VALUES
('1', '1', '2018-05-10 11:58:39'),
('2', '2', '2018-05-15 10:24:36'),
('3', '2', '2018-05-15 10:24:36');
然后,您将选择具有按朋友ID分组的最长时间的消息,然后您将选择其中包含最大id
的消息。您可以使用以下SQL实现这一目标。
SELECT MESSAGES.id, MESSAGES.friend_id, MESSAGES.message_time FROM
(
SELECT max(id) as id, friend_id FROM
(SELECT id,
m.friend_id,
m.message_time
FROM MESSAGES m
JOIN
(SELECT friend_id,
MAX(message_time) AS message_time
FROM MESSAGES
GROUP BY friend_id) mx ON mx.friend_id = m.friend_id
AND mx.message_time = m.message_time) b GROUP BY friend_id
) a JOIN
MESSAGES on a.id = MESSAGES.id ;
在上述查询中获取MESSAGES表所需的结果应该相当直接,因为你已经根据你的问题做了类似的事情。