2

我有一张包含用户之间消息列表的表格。表结构:

Receiver | Sender | Subject   | Message

Daniel   | Admin  | Title 1   | Message 1
Admin    | Daniel | Title 2   | Message 2
Daniel   | Admin  | Title 3   | Message 3
Daniel   | John   | Title 4   | Message 4

假设我以 Daniel 身份登录。我想显示与我通信的人员列表(无论我是发送者还是接收者),以及最新的主题/消息。就像是:

Admin | Title 3 | Message 3
John  | Title 4 | Message 4

我已经尝试了一百万种方法来做到这一点,但我就是不能把我的手指放在它身上!

如果有人可以帮助我区分我是发送者和接收者的最新消息(例如,某种 if 语句),那也会很有帮助。但是,我自己还没有尝试过,所以这不是一个紧迫的问题。

任何帮助将不胜感激 :)

4

4 回答 4

0
SELECT Receiver, Subject, Message FROM m WHERE Sender='<your_name>'
UNION ALL
SELECT Sender, Subject, Message FROM m WHERE Receiver='<your_name>'

This will simply return the list of names that had contact with given name. You can manipulate this code to also return whether it was a sender or receiver

Edit (after exchanging comments)

This will show you last message from each conversation, whether a person you're looking for was a sender or receiver. The code is based on my temporary table, you have to tweak it a little bit to suit your needs.

SELECT MAX(id), conv, message FROM (
  SELECT id , CONCAT(sender,'-',receiver) AS 'conversation', message FROM t
  WHERE
    sender = '<person>'
  GROUP BY rec
  UNION
  SELECT id , CONCAT(rec,'-',sender) AS 'conversation', message FROM t
  WHERE
    rec = '<person>'
  GROUP BY sender
) as t
GROUP BY convsersation, message

Explanation

  1. In first subquery I am getting latest message for each conversation where person you are looking for was a sender.

  2. Second subquery gives us the result of latest message for conversations where that person was a receiver.

  3. CONCAT is used so we can merge conversations together

  4. We wrap it in the main query that will output only MAX(id) which is the latest message.

  5. If you need to know whether person in question was receiver or sender, you can add another column in the subqueries:

    SELECT MAX(id), conversation, message, type FROM ( SELECT id , CONCAT(sender,'-',receiver) AS 'conversation', message, 'sender' AS 'type' FROM t WHERE sender = '' GROUP BY rec UNION SELECT id , CONCAT(rec,'-',sender) AS 'conversation', message, 'receiver' AS 'type' FROM t WHERE rec = '' GROUP BY sender ) as t GROUP BY convsersation

(for some reason I can't make a code block on this second query, could someone edit it please)

于 2012-12-18T09:22:00.330 回答
0

To get the output you specify....

SELECT i.correspondent, m.subject, i.msg
FROM (
SELECT sender AS correspondent, MAX(message) msg
FROM mesages
WHERE receiver='Daniel'
GROUP BY sender
UNION
SELECT receiver AS correspondent, MAX(message)
FROM mesages
WHERE sender='Daniel'
GROUP BY receiver )i,
messages m
WHERE i.msg=m.message;

But there are lots of other ways to get a list of correspondents.

于 2012-12-18T09:24:19.453 回答
0
SELECT Receiver, Subject, Message 
FROM Messages 
WHERE Sender='Daniel'
UNION
SELECT Sender, Subject, Message
FROM Messages
WHERE Receiver='Daniel';

这将返回“丹尼尔”发送和接收的所有消息。要对此进行扩展并指定哪些记录是发送或接收的消息,您可以将代码修改为以下内容,其中 0 与已发送消息相关,1 与已接收消息相关:

SELECT Receiver, Subject, Message, 0 AS 'MessageType'
FROM Messages 
WHERE Sender='Daniel'
UNION
SELECT Sender, Subject, Message, 1 AS 'MessageType'
FROM Messages
WHERE Receiver='Daniel';

然后,当您返回数据时,您可以根据 0/1 值将其分成两组。

我还建议创建一个表来包含您的用户,以便在您的 Messages 表中您可以通过他们的 ID 而不是仅仅通过他们的名字来引用他们。

于 2012-12-18T09:37:01.003 回答
0

试试这个查询

SELECT Receiver, Subject, Message ,'Sender' as 'type' FROM m WHERE Sender='Danial'
UNION ALL
SELECT Sender, Subject, Message ,'Reciever' as 'type'  FROM m WHERE Receiver='Danial'

编辑:

(SELECT Receiver, Subject, Message ,'Sender' as 'type' FROM m WHERE Sender='Danial' ORDER By message desc limit 1)
UNION ALL
(SELECT Sender, Subject, Message ,'Reciever' as 'type'  FROM m WHERE Receiver='Danial'ORDER By message desc limit 1)
于 2012-12-18T09:34:28.713 回答