开发者

Database, query implementation

I have a table named USERS:  
userID  userName
1       Foo
2       Bar
3       Foobar

I also 开发者_如何学编程have a table named MAIL:
mailID    senderID   recipientID   subject     messageBody
1          1           2            Hello      testtesttest
2          1           3            Hello2     test1test1test1

I have a form with fields From, To, Subject and Message Body. My Question is, how can i implement a query in order to return an entry from MAIL table say entry mailID = 1 to fill the fields with the actual names contained in userName, the subject and the message body?


SELECT m.mailid,
       m.subject,
       sender.userName,
       recepient.userName
FROM mail m
  JOIN users sender ON m.senderID = sender.userID
  JOIN users recepient ON m.recepientID = recepient.userID


That will be (sans syntactic peculiarities of your particular DBMS)

select s.userName, r.userName, m.subject, m.messageBody 
    from mail m 
    inner join users s on m.senderID = s.ID
    inner join users r on m.senderID = r.ID


select username, subject, messageBody from Mail m inner join users u on m.recipientid (or senderid) = u.userid where entryid = 1

that should do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜