Opposite of inner join

后端 未结 4 1182
清歌不尽
清歌不尽 2021-01-30 14:16

What will be the opposite of inner join? For a table table Person (int PersonId, varchar PersoName, int AddrId), I want to know the rows in Person with bad Ad

4条回答
  •  长情又很酷
    2021-01-30 14:53

    I think the best solution would be using EXISTS. Like this:

    SELECT * FROM Person P
    WHERE P.AddrId IS NOT NULL 
      AND NOT EXISTS ( SELECT 1 FROM Address A WHERE A.AddrId = P.AddrId )
    

    The query above will return every person that the AddrId is set but does not have a corresponding record in the Address table.

    Obs.: Use the constant 1 in the EXISTS query to avoid table access.

提交回复
热议问题