2

我的消息传递架构是这样的

USER  
id  
name 


MESSAGE  
id  
messagetitle    
sender_id    
receiver_id  

我想显示messagetitle发件人和收件人姓名

例如 MySQL 查询应该显示:

+--------------+------------+--------------+
| MessageTitle | SenderName | Recievername |
+--------------+------------+--------------+
| Important    | Raj        | Vijay        |
| Solution     | Vijay      | Raj          |
+--------------+------------+--------------+
4

2 回答 2

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 小提琴演示

于 2013-08-10T13:46:28.050 回答
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
于 2013-08-10T13:49:12.403 回答