MYSQL - Select only if row in LEFT JOIN is not present

前端 未结 2 504
一个人的身影
一个人的身影 2021-01-07 20:19

I have 2 simple mysql tables. The first 1 called mail and has 2 rows:

sender | receiver
Marley | Bob 
Saget  | Bob 

The second one called b

2条回答
  •  被撕碎了的回忆
    2021-01-07 20:46

    The left join will produce null rows for the mismatches.
    It's those null rows that you need to filter on.

    SELECT * FROM mail  
    LEFT JOIN block ON (block.blocker = 'Bob') 
    WHERE block.blocker IS NULL
    

    It's kind of strangle to be joining on a fixed value however, a more common join (given your tables) would be:

    SELECT * FROM mail  
    LEFT JOIN block ON (block.blocker = mail.receiver
                    and block.blocked = mail.sender)<<-- these should match
    WHERE block.blocker IS NULL                     <<-- select only mismatches
    AND mail.receiver like 'bob';
    

提交回复
热议问题