Yangrui
Yangrui

Reputation: 1247

SQL expression to get names of friends

I am making a get friends network function using SQL. I used the following code:

SELECT relationship.requester, 
       relationship.requested, 
       user.firstname, 
       user.lastname 
  FROM relationship 
  JOIN user ON (user.guid = relationship.requester
           OR user.guid = sqrelationship.requested) 
 WHERE relationship.requester = {$userid} 
    OR relationship.requested = {$userid} 
   AND relationship.approved = 1

The SQL will return a table with the IDs of approved relationships either initiated or received by user. However, the user.firstname and user.lastname return me only one set of results. How can I make this expression able to get both requester and requested's names?

Upvotes: 4

Views: 73

Answers (1)

Hogan
Hogan

Reputation: 70523

Hard to tell from the question where you don't specify the design of your tables, but I'm guessing like this:

select relationship.requester, relationship.requested, 
       requester.firstname, requester.lastname,
       requested.firstname, requested.lastname,
from relationship
left join user as requester on user.guid = relationship.requester 
left join user as requested on user.guid = relationship.requested
where relationship.requester = {$userid}
  and relationship.approved = 1

Upvotes: 1

Related Questions