2012-12-26 189 views
2

我有一个问题从用户之间存储消息的表中检索数据。 下面是表:根据第一条消息获取最新的未读消息

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: '保罗', '约翰', '不,我是拆包我的礼物',0,2012-12-26 9点05分00秒
  • message id 9:'Tim','George','真的很好',0,2012-12-22 10:15:00
  • message id 11:'Tim','John','That is a ',0,2012-12-27 15:20:00

我做了一个查询,它几乎是,但消息id 11的行缺少:

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 

由于子查询由the_other分组,因此缺少消息ID 11。 如果我按发件人和收件人分组“发件人”,它将检索邮件标识3,但它是不需要的行。

什么是应该解决我的问题的SQL?

+1

做什么你的意思是发起?为什么不包括记录ID:'3'? –

+0

在启动时,我的意思是一个对话的第一条消息是由经理发送的,所以John或George。 记录3不能包含,因为在这个对话中,第一条消息是由Alice发送的,而不是由George发送的。 在我的例子中,有4个对话,爱丽丝和乔治,约翰和保罗,乔治和蒂姆,约翰和蒂姆。最后3次对话是唯一由经理发起的对话。 – Shaolin

回答

0

下面可以使用查询:

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 
+0

查询语法错误 –

+0

是的,我们不能选择所有记录通过分组只有发件人.. – Mari

+0

我fiexd您的查询错误,但它给了我一个错误的结果:http://sqlfiddle.com/#!2/30f5a/ 7/0 – Shaolin

0

最后,我已经找到了解决办法,但我不认为这是最优化的查询:

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 

http://sqlfiddle.com/#!2/30f5a/17/0