How do I 'subtract' sql tables?

后端 未结 14 1960
死守一世寂寞
死守一世寂寞 2020-12-05 13:12

Its not really a subtraction I\'m looking for. And I know its not a union or intersection... I have been given a long and complex stored procedure that returns a table of

相关标签:
14条回答
  • 2020-12-05 14:00
    SELECT both.*
    FROM both LEFT OTUER JOIN inactives USING (whatever_id)
    WHERE inactives.whatever_id IS NULL;
    

    or

    SELECT * FROM both
    EXCEPT
    SELECT * FROM inactives;
    
    0 讨论(0)
  • 2020-12-05 14:07
    SELECT * FROM Table1 
    LEFT JOIN Table2 on Table1.id = Table2.id
    WHERE Table2.id IS NULL
    

    this should work on almost any database engine

    0 讨论(0)
  • 2020-12-05 14:10

    In MS TSql, I think you want the EXCEPT keyword.

    query1 EXCEPT query2
    

    Which will return all rows found in the first query that are not also found in the second query.

    0 讨论(0)
  • 2020-12-05 14:10

    You can just used the first sp that return the Active & Inactive and in WHERE cluse put condition for the document status =inactive, you wil get inactive document only.

    0 讨论(0)
  • 2020-12-05 14:12

    Assuming there are unique IDs that correspond across the two tables:

    select * from table_both b
    where not exists (select * from table_active a where a.id = b.id)
    
    0 讨论(0)
  • 2020-12-05 14:12

    I believe EXCEPT is what you are looking for. Syntax is similar to UNION or INTERSECT.

    0 讨论(0)
提交回复
热议问题