sql - MySQL How to account for blocked users when returning comments -
i thought had solved problem, realised specifying user not friends in friends table cause user not able see posts, not behaviour want.
a blocked user has category of 4. i'm aware return comments post , manually check if category of user 4, un-necessary computation solved single query (i think).
select distinct ent.entity_id, ent.profile_pic_url, ent.first_name, ent.last_name, ent.last_checkin_place, comments.content, friends.category checkin_comments comments join entity ent on comments.entity_id = ent.entity_id join friends on comments.entity_id = friends.entity_id1 or comments.entity_id = friends.entity_id2 comments.chk_id = 1726 , friends.category != 4 group comments.comment_id
this return results, because there no way specify logged in user. thought supply sub-query:
select distinct ent.entity_id, ent.profile_pic_url, ent.first_name, ent.last_name, ent.last_checkin_place, comments.content, friends.category checkin_comments comments join entity ent on comments.entity_id = ent.entity_id join friends on comments.entity_id = friends.entity_id1 or comments.entity_id = friends.entity_id2 comments.chk_id = 1726 , friends.category = ( select category friends friends.entity_id1 = 1527 , friends.entity_id2 = comments.entity_id or friends.entity_id1 = comments.entity_id , friends.entity_id2 = 1527 ) -- filter out blocked users have category of 4 , friends.category != 4 group comments.comment_id
the idea here fetch category of each user , filter result set based on that, sort of worked, in sense returns list of users friends, not account use case when comment made non-friend (who won't exist in friends table).
how can fix query users returned, , users have category of 4 not shown?
if need more information please ask.
you're looking outer join. variant of first query should trick:
select distinct ent.entity_id, ent.profile_pic_url, ent.first_name, ent.last_name, ent.last_checkin_place, comments.content, friends.category checkin_comments comments join entity ent on comments.entity_id = ent.entity_id left join friends on comments.entity_id = friends.entity_id1 or comments.entity_id = friends.entity_id2 comments.chk_id = 1726 , ifnull(friends.category, 0) != 4 group comments.comment_id
in event comment author not associated requester via friends
table, comment included in join result, columns derived friends
being null
. ifnull()
in where
clause deals converting null
category value 0
(though in fact value other 4
work).
Comments
Post a Comment