December 7, 2007 at 3:22 am
Hi ,
I have two tables named users and friends...
In users table i have fields like userid,username,picture(path from
where the picture was downloaded)
In friends table I have fields like friend_id,userid,request_status(accepted or rejected)
how to retrieve the names of the friends...
I want the output to have fields like Userid,UserName,Friend_id,
along with friend's name.But the friends table does not contain friendname...Could anyone help me on this query
Thanks in advance
December 7, 2007 at 3:43 am
Is the friend_id a reference to users table? (which i think should)
SELECTT.userid, U.username, T.friend_id, F.username As friendname, T.request_status
FROMfriends T
INNER JOIN users U ON T.userid = U.userid
INNER JOIN users F ON T.friend_id = F.userid
--Ramesh
December 7, 2007 at 3:54 am
I'm not sure if I'm missing something here, as the query seems pretty straightforward. You don't, however, include details about how the tables link to each other, so I'll have to make some assumptions.
You need to join friends to an aliased user table something like this:
select u1.userid, u1.username, f.friend_id, u2.username friendname
from users u1
inner join friends f on f.userid= u1.userid
inner join users u2 on u2.userid = f.friend_id
December 7, 2007 at 3:56 am
Thanks a lot
It's working fine
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply