列出发送或接收消息的所有用户

时间:2012-12-25 08:38:10

标签: mysql

我有一张表“消息”。

字段是:

- id (primary key, int, auto increment)
- uid (the user who sent the message)
- rid (the user who received the message)
- text (text of the message)
- time (time of message)
- read (read or unread. 0 is unread, 1 is read).

作为用户,我可以是UID或RID。因此,如果我想显示我发送/接收消息的用户列表,我该怎么办?此外,每个用户只应显示一次。请帮忙。谢谢。 :)

2 个答案:

答案 0 :(得分:2)

一种可能的方法是使用UNION(而不是UNION ALL),以便删除重复的行:

SELECT uid FROM messages WHERE rid = 'me'
UNION
SELECT rid FROM messages WHERE uid = 'me'

答案 1 :(得分:0)

我认为,你的桌面设计看起来像这样,

ID | UID | RID |TEXT |TIME | READ

所以,我相信,根据你的桌面设计,你可以使用这个

SELECT * FROM messages WHERE rid = 'me' OR uid='me'