SELECT post.id FROM list_posts AS post LEFT OUTER JOIN private ON (post.id = private.pid) LEFT OUTER JOIN official_user AS adminu LEFT OUTER JOIN count_comments AS ccount ON (post.id = ccount.pid) WHERE post.id = %s AND( adminu.user = %s --User is the official_user OR post.private = false --Post is not private OR private.uid IS NOT null --User is allowed to view post ) LIMIT 1