What is the standard SQL Query to retrieve the intersection of tables?

前端 未结 9 553
谎友^
谎友^ 2021-02-02 03:57

Selecting the union:

select * from table1 
union 
select * from table1_backup 

What is the query to select the intersection?

相关标签:
9条回答
  • 2021-02-02 04:32
    SELECT *
    FROM table1
    WHERE EXISTS
    (SELECT *
    FROM table1_backup
    WHERE table1.pk = table1_backup.pk)
    

    works

    0 讨论(0)
  • 2021-02-02 04:36

    subqueries?! really?

    to get the intersection of table1 and table2:

    SELECT * FROM table1, table2 WHERE table1.pk=table2.pk;
    
    0 讨论(0)
  • 2021-02-02 04:39

    In SQL Server intersect

    select * from table1 
    intersect
    select * from table1_backup
    
    0 讨论(0)
  • 2021-02-02 04:43

    "intersect" is also part of standard SQL.

    Inner join gives a different answer.

    0 讨论(0)
  • 2021-02-02 04:45

    An intersect on two identical tables a and b can be done in this manner:

    SELECT a.id, a.name
    FROM a INNER JOIN b
    USING (id, name)
    
    0 讨论(0)
  • 2021-02-02 04:48
    select distinct * from (select * from table1 union select * from table1_backup) 
    
    0 讨论(0)
提交回复
热议问题