MYSQL PHP Query: Joining on more than one field
My table scheme for a social network site is as follows:
users
id|username
userinfo
id|userid|handle|description
network
id|inviterid|invitedid|status
When one person invites another to join his/her network, an entry is made
into the network table with status 1. If the request is accepted, the
status goes to 2. So far so good.
However, I'm having trouble listing out members of someone's network while
joining with other tables since in effect I have to join on more than one
field since the person can either be the inviter or the invited.
Here is query to get members of network //$id = id of logged in user $sql
= "SELECT * FROM network n LEFT JOIN userinfo ui on n.invitedid= ui.userid
OR n.inviterid = ui.userid LEFT JOINusers u on n.invitedid= u.id OR
n.inviterid = n.id WHERE status='2'
Not all users have userinfo records as not everyone has filled out a
profile. But I want to get these as well.
Above query gives multiple records for users with profiles, records for
the logged in user and excludes any without profile. I want to exclude
logged in user and then one record for each member of his/her network
including members without profiles.
Thanks for any suggestions.
No comments:
Post a Comment