You only want records from wallPost
. I would suggest that you remove Connections
table in the from
clause and put the logic in subqueries using exists
:
select wp.*
from wallPost wp
where wp.user_id = x or
exists (select 1 from Connections c where c.user1_id = x and wp.user_id = c.user2_id) or
exists (select 1 from Connections c where c.user2_id = x and wp.user_id = c.user1_id)
order by <field_name>;
For best performance, you would want two indexes, one on Connections(user2_id, user1_id)
and the other on Connections(user1_id, user2_id)
.