我的消息传递模式就像这样
USER
id
name
MESSAGE
id
messagetitle
sender_id
receiver_id
我想显示带有发件人和收件人姓名的messagetitle
例如,MySQL查询应该显示:
+--------------+------------+--------------+
| MessageTitle | SenderName | Recievername |
+--------------+------------+--------------+
| Important | Raj | Vijay |
| Solution | Vijay | Raj |
+--------------+------------+--------------+
答案 0 :(得分:3)
为您的解决方案尝试以下sql:
SELECT
m.messagetitle AS MessageTitle
,u1.Name AS SenderName
,u2.Name AS Recievername
FROM MESSAGE m
INNER JOIN USER u1 ON u1.id = m.sender_id
INNER JOIN USER u2 ON u2.id = m.receiver_id
<强> SQL FIDDLE DEMO 强>
答案 1 :(得分:1)
应该是一个简单的旧简单连接,写意像;
SELECT m.messagetitle, s.name sender, r.name receiver
FROM message m
JOIN user s ON m.sender_id=s.id
JOIN user r ON m.receiver_id=r.id