首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求两个表列与行的差异对比的sql 语句,该怎么解决

2012-05-28 
求两个表列与行的差异对比的sql 语句有两个两个表table1table2table1如下:row1row2aobpcqdstable2如下row1

求两个表列与行的差异对比的sql 语句
有两个两个表table1 table2
table1如下:
  row1 row2
  a o
  b p 
  c q
  d s 

table2如下
  row1 row2
  a o
  b p
  c x 
  d y
  e z

我想找出这两个边的差异并输出 table3
结果应该为
  row1 row2
  c x 
  d y
  e z

这个sql 语句该怎么写,




[解决办法]

SQL code
select * from table1 a inner join table2 b on a.row1 = b.row1 and a.row2<>b.row2
[解决办法]
SQL code
select * from tb2 except select * from tb1
[解决办法]
select * table2 a where not exists(select 1 from table1 b where a.col1=b.col1 and a.col2<>b.col2)
[解决办法]
SQL code
select * from tb2 a where not exists(select 1 from tb1 b where a.row1=b.row1 and a.row2=b.row2)
[解决办法]
SQL code
--> 测试数据:[table1]if object_id('[table1]') is not null drop table [table1]create table [table1]([row1] varchar(1),[row2] varchar(1))insert [table1]select 'a','o' union allselect 'b','p' union allselect 'c','q' union allselect 'd','s'--> 测试数据:[table2]if object_id('[table2]') is not null drop table [table2]create table [table2]([row1] varchar(1),[row2] varchar(1))insert [table2]select 'a','o' union allselect 'b','p' union allselect 'c','x' union allselect 'd','y' union allselect 'e','z'select * from table2 a--给table2取别名awhere not exists(select 1 from table1 b --给table1取别名awhere a.row1=b.row1 and a.row2=b.row2)/*row1    row2c    xd    ye    z*/ 

热点排行