Mysql 根据第一条消息获取最新未读消息
从存储用户之间消息的表中检索数据是一个问题。 这是表格:Mysql 根据第一条消息获取最新未读消息,mysql,sql,Mysql,Sql,从存储用户之间消息的表中检索数据是一个问题。 这是表格: CREATE TABLE messages ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, sender VARCHAR(20), recipient VARCHAR(30), content TEXT, is_read TINYINT(1), created_at DATETIME, PRIMARY KEY (`id`) , IN
CREATE TABLE messages
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sender VARCHAR(20),
recipient VARCHAR(30),
content TEXT,
is_read TINYINT(1),
created_at DATETIME,
PRIMARY KEY (`id`) ,
INDEX idx_sender (sender) ,
INDEX idx_recipient (recipient)
);
INSERT INTO messages (id, sender, recipient, content, is_read, created_at)
VALUES
-- Alice and George
(1, 'Alice', 'George', 'Happy new year', 1, '2012-12-24 23:05:00'),
(2, 'George', 'Alice', 'It is chrimas night...', 1, '2012-12-24 23:10:00'),
(3, 'Alice', 'George', 'Happy Xmas then', 0, '2012-12-25 00:00:00'),
-- John and Paul
(4, 'John', 'Paul', 'Hi Paul', 1, '2012-12-26 09:00:00'),
(5, 'Paul', 'John', 'Hi John', 1, '2012-12-26 09:05:00'),
(6, 'John', 'Paul', 'Have you done this ?', 1, '2012-12-26 09:10:00'),
(7, 'Paul', 'John', 'No I was unpacking my gifts', 0, '2012-12-26 09:05:00'),
-- George and Tim
(8, 'George', 'Tim', 'How was the end of the world ?', 1, '2012-12-22 10:10:00'),
(9, 'Tim', 'George', 'Really nice !', 0, '2012-12-22 10:15:00'),
-- John and Tim
(10, 'John', 'Tim', 'I don\'t know if I should fire you for new year\'s eve', 1, '2012-12-27 15:20:00'),
(11, 'Tim', 'John', 'That is a great idea!', 0, '2012-12-27 15:20:00');
假设乔治和约翰是经理,其他人是雇员。
我需要检索经理是收件人的最新未读邮件。
同时,我必须只检索那些同时也是对话发起人的消息
因此,结果必须是:
- 消息id 7:'保罗','约翰','不,我正在打开我的礼物',01212-2609:05:00
- 消息id 9:'蒂姆','乔治','真不错',01212-22 10:15:00
- 信息id 11:‘蒂姆’、‘约翰’、‘这是个好主意!’,2012年12月27日15:20:00
SELECT
m.*
FROM
messages m
INNER JOIN (
-- retrieve the first message (fm)
SELECT t.sender, t.recipient
FROM (
SELECT id, sender, recipient, content, created_at,
IF ( sender IN ('John', 'George'), sender, recipient ) AS the_other
FROM messages
WHERE
sender IN ('John', 'George')
OR recipient IN ('John', 'George')
ORDER BY
created_at ASC
) t
GROUP BY
the_other
HAVING t.sender IN ('John', 'George')
) fm ON fm.recipient = m.sender
AND fm.sender = m.recipient
WHERE
m.recipient IN ('John', 'George')
AND m.is_read = 0
ORDER BY
m.created_at DESC
LIMIT 0, 10
缺少消息id 11,因为子查询正在由\u other进行分组。
如果我按发件人和收件人拆分“分组依据”,它将检索邮件id 3,但这是不需要的行
解决我的问题的SQL应该是什么?可以使用下面的查询:
select a.* from
(select id, sender, recipient, content, is_read, max(created_at)
from message where recipent in ('John', 'George') and is_read = 0
group by sender) a, message b (select id, sender, recipient,
content, is_read, min(created_at)
from message where sender in ('John', 'George') group by recipient) b
where a.sender=b.recepient
最后我找到了一个解决方案,但我认为这不是最优化的查询:
SELECT
m.*
FROM
messages m
INNER JOIN (
-- retrieve the first messages (fm)
SELECT t.sender, t.recipient
FROM (
SELECT id, sender, recipient, content, created_at,
IF ( sender = 'John', recipient, sender ) AS the_other
FROM messages
WHERE
sender = 'John'
OR recipient = 'John'
ORDER BY
created_at ASC
) t
GROUP BY
the_other
HAVING t.sender = 'John'
UNION
SELECT t.sender, t.recipient
FROM (
SELECT id, sender, recipient, content, created_at,
IF ( sender = 'George', recipient, sender ) AS the_other
FROM messages
WHERE
sender = 'George'
OR recipient = 'George'
ORDER BY
created_at ASC
) t
GROUP BY
the_other
HAVING t.sender = 'George'
) fm ON fm.recipient = m.sender
AND fm.sender = m.recipient
WHERE
m.recipient IN ('John', 'George')
AND m.is_read = 0
ORDER BY
m.created_at DESC
LIMIT 0, 10
你所说的“发起”是什么意思?为什么不包括记录ID:
3
?在initiated中,我的意思是对话的第一条消息是由经理John或George发送的。记录3不能包括在内,因为在此对话中,第一条消息是由Alice而不是George发送的。在我的例子中有4个对话,爱丽丝和乔治,约翰和保罗,乔治和蒂姆,约翰和蒂姆。最后3次对话是经理发起的唯一对话。我确认了您的查询错误,但它给了我错误的结果:我不想要记录3,记录11缺失