user3259283
user3259283

Reputation: 95

Using My SQL joins

I am having trouble finishing my MySQL JOIN. I am unsure of the syntax for the last part of my query.

My Query:

$posts_query= "
SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE (r.status = 1 OR r.status = 2)
AND (r.sender = '".$user_id."' OR p.user_id = '".$user_id."')


// How do I write this part?
AND skip where r.status = 1 and p.privacy = 2 where p.user_id != $user_id // 

ORDER BY p.post_id DESC;
";

Upvotes: 1

Views: 63

Answers (2)

Damien Black
Damien Black

Reputation: 5647

Instead of 'skip', just set it so that the value isn't true, using NOT:

SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE (r.status = 1 OR r.status = 2)
AND (r.sender = '$user_id' OR p.user_id = '$user_id')
AND NOT (r.status = 1 AND p.privacy = 2 AND p.user_id != '$user_id')
ORDER BY p.post_id DESC;"

Upvotes: 2

Maheswaran Ravisankar
Maheswaran Ravisankar

Reputation: 17920

SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE r.status in (1,2)
AND (r.sender = '$user_id' OR p.user_id = '$user_id')
AND NOT ( r.status = 1 and p.privacy = 2 AND p.user_id != '$user_id' )
ORDER BY p.post_id DESC;

Upvotes: 1

Related Questions