php - Multiple inner joins in MySql not working -


i have 3 tables :

useractivitylog

id   userid   activity_type  activity_id   date 

which store user activities.

requestfriend

opid  userid  friendid  requesttime  message  source  requesttype  friendemail  status 

which store friends details. userid , friendid can user's id. if user has send friend's request user b, userid contain id of , friendid id of b. anyway b friend of , friend of b.

auth_user_profiles

id  user_id  first_name  last_name  profileimage 

which stores user profile information.

what need select friends activities useractivitylog table. tried following query. didnt worked.

    echo "select ua.*,rf.opid,aup.user_id,aup.first_name,aup.last_name,         aup.profileimage useractivitylog ua inner join requestfriend rf     inner join auth_user_profiles aup on    aup.user_id= if (rf.userid='$userid',rf.friendid,rf.userid)     (rf.userid=$userid or rf.friendid=$userid)    , rf.status='2' , ua.userid!=$userid"; 

here, $userid user id. want retrieve profile information , activities of users friends of mine. know, returning rows in useractivitylog table if userid not in friends list.

can me find appropriate query this. in advance.

you don't join properly. condition useractivitylog being linked other tables user id not $userid. true records in table, cartesian product (cross join).

btw: don't join auth_user_profiles either, because compare string '$userid' instead of comparing $userid.

let's re-write statement such retrieve friends first , join other tables these:

select    ua.*,   friend.opid,   aup.user_id,   aup.first_name,   aup.last_name,   aup.profileimage   (   select distinct     case when userid = $userid friendid else userid end id,     opid   requestfriend   $userid in (userid, friendid) , status = '2' ) friend inner join useractivitylog ua on ua.userid = friend.id inner join auth_user_profiles aup on aup.user_id = friend.id; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -