mysql - Join returning zero elements where it should not -
the below sql statement looks friendship between 2 user elements, querying friendship table using users' profile_id#s, line added working statement left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2'
want see if user abcde2 has blocked anyone, , if want filter these friendships tableon clause left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2'
have block table populated block row of user abcde2 friended user element, whole statement returning 0 rows. please me fix if can. thank you
select users1.username firstusername, users2.username secondusername, users1.profile_id firstprofid, users2.profile_id secondprofid, users1.picup firstpicup, users2.picup secondpicup `users` left outer join `friendship` on friendship.profile_id_1 = users.profile_id or friendship.profile_id_2 = users.profile_id left outer join users users1 on users1.profile_id = friendship.profile_id_1 left outer join users users2 on users2.profile_id = friendship.profile_id_2 left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2' users.profile_id = 'abcde2' , blockedusers.profile_id_1 != 'abcde2' , friendship.state = 1 limit 6
edit: answer , comments, unfortunately after trying null, it's still returns 0 rows, thinking should checking blockedusers.profile_id_2 instead , separate 2 possibilities of friendship table's first user existance (in either friendship.profid_1 or _2), , combine them union, has weirder results, need head together...
select users1.username firstusername, users.username secondusername, users1.profile_id firstprofid, users.profile_id secondprofid, users1.picup firstpicup, users.picup secondpicup `users` left outer join `friendship` on friendship.profile_id_2 = users.profile_id left outer join users users1 on users1.profile_id = friendship.profile_id_1 left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2' users.profile_id = 'abcde2' , blockedusers.profile_id_2 != friendship.profile_id_1 , friendship.state = 1 union select users.username firstusername, users.username secondusername, users.profile_id firstprofid, users2.profile_id secondprofid, users.picup firstpicup, users2.picup secondpicup `users` left outer join `friendship` on friendship.profile_id_1 = users.profile_id left outer join users users2 on users2.profile_id = friendship.profile_id_2 left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2' users.profile_id = 'abcde2' , blockedusers.profile_id_2 != friendship.profile_id_2 , friendship.state = 1
i think looking for. last line of last join should match on second id in friendship. if there match, omitted blockedusers.profile_id_1 null
criteria in clause.
select users1.username firstusername, users2.username secondusername, users1.profile_id firstprofid, users2.profile_id secondprofid, users1.picup firstpicup, users2.picup secondpicup friendship left outer join users users1 on users1.profile_id = friendship.profile_id_1 left outer join users users2 on users2.profile_id = friendship.profile_id_2 left outer join block_user_filters blockedusers on blockedusers.profile_id_1 = 'abcde2' , blockedusers.profile_id_2 in (users1.profile_id, users2.profile_id) (friendship.profile_id_1 = 'abcde2' or friendship.profile_id_2 = 'abcde2') , friendship.state = 1 , blockedusers.profile_id_1 null limit 6
Comments
Post a Comment