Opposite of inner join

后端 未结 4 1167
清歌不尽
清歌不尽 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:42

    An Inner join is not diametric to an Outer Join. They serve different purposes. However, a common pattern to find rows from one table that do not exist in another is to use an Outer Join:

    Select ...
    From Table1
        Left Join Table2
            On Table2.ForeignKeyCol = Table1.PrimaryKeyCol
    Where Table2.PrimaryKeyCol Is Null
    

    This returns all rows from Table1 and any matching rows from Table2 such that if a given Table1 row has no Table2 match, a null for the Table2 columns are returned. By then requiring that a non-nullable column (Table2.PrimaryKeyCol) be Null, I will get all rows from Table1 that do not exist in Table2. Using your example table names we would have something like:

    Select ...
    From Person
        Left Join Address
            On Address.PersonId = Person.Id
    Where Address.Id Is Null
    

提交回复
热议问题