MYSQL Query with 2 columns in Table A related to 1 column in Table B
I ha开发者_高级运维ve 2 Tables, User and Mail.
In User Table i have 2 columns that i will use, the ID column which makes the relation with the Mail Table and it is the Index of User Table and the Name column.
In Mail Table i have Receiver Column and Sender Column. Both columns, Receiver and Sender have a number that relates to the ID Column in the User Table. In the User Table is where the name columns resides and i want to make a query that shows me the Receiver and Sender Columns but with the name of the user, not the ID.
Up to this point i have this:
SELECT name AS Send, name AS Receive FROM mail,user WHERE sender=guid;
I know there is still a part of the query missing but i can not figure out what else to put to tell it to show in the SEND output column the name of the sender and in the RECEIVE output column the name of the receiver.
You need to join on your User table twice. Your query will look something like this:
SELECT u1.name as send, u2.name as receive
FROM mail m
INNER JOIN user u1 on m.sender = u1.guid
INNER JOIN user u2 on m.receiver = u2.guid
That should do it for you!
精彩评论