0

假设我有两张桌子。例如,

表格1:

Store Product 
 1       2    
 1       1
 2       3
 2       4

和表 2

Store Product 
 1       2    
 2       3

如何删除表 1 中也包含在表 2 中的所有行?

因此,新的表 1 将是:

Store Product 
 1       1
 2       4
4

2 回答 2

3

你似乎想要:

select t1.*
from table1 t1
where not exists (select 1 
                  from table2 t2
                  where t2.store = t.store and 
                        t2.product = t1.product
                 );

同样删除版本将是:

delete 
from table1 t1
where exists (select 1 
              from table2 t2 
              where t2.store = t.store and 
                    t2.product = t1.product
             ); 
于 2018-08-01T17:25:03.317 回答
1

你可以使用这样的东西:

DELETE
    t1
FROM
    table1 t1
WHERE
    (t1.store, t1.product) IN ( SELECT 
                                     t2.store, 
                                     t2.product 
                                from 
                                    table2 t2);
于 2018-08-01T17:29:25.287 回答