MySQL query NOT something

This is the MySQL query I am running:

SELECT * 
    FROM wp_social_stuff_events 
    WHERE `user_1_id` = 63 
        OR `user_1_id` = 143 
        OR `user_2_id` = 63 
        OR `user_2_id` = 143 
    ORDER BY `event_timestamp` DESC LIMIT 100

How would I use that query but say I don’t want any results where “user_1_id” is 53? I tried using AND user_1_id NOT 53 at the end but I can’t seem to get that to work either (not sure if it is the correct way)?

Read More

Any ideas?

Related posts

Leave a Reply

4 comments

  1. SELECT * 
    FROM wp_social_stuff_events 
    WHERE (`user_1_id` = 63 
    OR `user_1_id` = 143 
    OR `user_2_id` = 63 
    OR `user_2_id` = 143)
    AND user_1_id <> 53
    ORDER BY `event_timestamp` DESC LIMIT 100
    

    You can use != or <> for not equal as you can read in the MySql documentation. It’s also require to have parentheses because AND is executed before OR. See MySql Operator Precedent.

  2. SELECT * 
        FROM wp_social_stuff_events 
        WHERE `user_1_id` = 63 
            OR `user_1_id` = 143 
            OR ((`user_2_id` = 63 OR `user_2_id` = 143) 
             AND `user_1_id` <> 53) 
        ORDER BY `event_timestamp` DESC LIMIT 100