现有如下两张表(共3列,3列合为一个主键):

 

表1:                                 表2:

--------------                     --------------

列1 列2 列3                         列1 列2 列3

--------------                     --------------

 1 a1 b1                               1 a1 b2

 1 a2 b2                               1 a2 b2

 2 a6 b6                               3 a6 b6

 

现在希望得到表1中有的但是表2中没有的记录,即获得两表差集,该如何获取呢?

即得出来的结果应该是

--------------

列1 列2 列3

--------------

 1 a1 b1

 2 a6 b6

 

在sql 2000中可以这样实现:

select * from 表1 as t1 

where not exists (select * from 表2 as t2 where t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3)

 

在sql 2005中可以这样实现,因为2005中多了except(SQL 2000没有),就简单很多了:

select * from 表1

except

select * from 表2

 

相关文章:

  • 2021-07-10
  • 2022-02-07
  • 2021-07-23
  • 2021-09-15
  • 2022-12-23
  • 2021-08-23
  • 2021-11-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
相关资源
相似解决方案