I have the following three tables:

items
id
title
brief_description
user_id

community
user_id
friend_id
type (0 | 1) if type == 1 : you are friends, if type == 0: tracking the user

item_invitations
item_id
user_id

Right now, I use this code:

$sql = 'SELECT i.id, i.title, i.brief_description FROM items i INNER JOIN community c ON i.user_id = c.friend_id WHERE c.user_id = ' . $_SESSION['user_id'] . ' AND i.visibility = 1';

That shows me all items in my community where the visibility == 1.

visibility = 1 means that everyone can see it.

visibility = 2 means only friends can see it.

visibility = 3 means you need an invitation to see it.

I want to include visibility 2 and 3 in the query, if possible.

Below is some pseudo code on how to do that but i really don't know how to implement this in SQL.

if i.visibility == 2
then community.type must be 1
if i.visibility == 3
then item_invitations.item_id = i.id AND item_invitations.user_id = $_SESSION['user_id']

Thanks