How to return rows from left table not found in right table?

前端 未结 7 923
滥情空心
滥情空心 2020-12-23 09:09

I have two tables with similar column names and I need to return records from the left table which are not found in the right table? I have a primary key(column) which will

相关标签:
7条回答
  • 2020-12-23 09:47

    This page gives a decent breakdown of the different join types, as well as venn diagram visualizations to help... well... visualize the difference in the joins.

    As the comments said this is a quite basic query from the sounds of it, so you should try to understand the differences between the joins and what they actually mean.

    Check out http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

    You're looking for a query such as:

    DECLARE @table1 TABLE (test int)
    DECLARE @table2 TABLE (test int)
    
    INSERT INTO @table1
    (
        test
    )
    SELECT 1
    UNION ALL SELECT 2
    
    INSERT INTO @table2
    (
        test
    )
    SELECT 1
    UNION ALL SELECT 3
    
    -- Here's the important part
    SELECT  a.*
    FROM    @table1 a
    LEFT    join @table2 b on a.test = b.test -- this will return all rows from a
    WHERE   b.test IS null -- this then excludes that which exist in both a and b
    
    -- Returned results:
    
    2
    
    0 讨论(0)
提交回复
热议问题