T-SQL: Comparing Two Tables - Records that don't exist in second table

前端 未结 7 1307
小鲜肉
小鲜肉 2021-01-02 05:05

If UNION ALL is an addition in T-SQL. What is the equivalent of subtraction?

For example, if I have a table PEOPLE and a table

7条回答
  •  伪装坚强ぢ
    2021-01-02 05:47

    When I compare tables looking for data that isn't in one that is in the other I typically use SQL Division.

    select *(or selected matching field) 
    from tableA as A
    where not exist
    (select *(or selected matching field) 
    from tableB as B 
    where A.key = B.key)
    

    This query will return the results that are in tableA that are not in through the process of division.

    select *(or selected matching field) 
    from tableA as A
    where exist
    (select *(or selected matching field) 
    from tableB as B 
    where A.key = B.key)
    

    This query will return all the rows of data that match in both tables therefore if there is a row data that is in tableA that isn't in tableB that row of data will not be retrieved.

提交回复
热议问题