34,590
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[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 all
select 'b','p' union all
select 'c','q' union all
select '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 all
select 'b','p' union all
select 'c','x' union all
select 'd','y' union all
select 'e','z'
select * from table2 a--给table2取别名a
where not exists(select 1 from table1 b --给table1取别名a
where a.row1=b.row1 and a.row2=b.row2)
/*
row1 row2
c x
d y
e z
*/
--> 测试数据:[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 all
select 'b','p' union all
select 'c','q' union all
select '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 all
select 'b','p' union all
select 'c','x' union all
select 'd','y' union all
select 'e','z'
select * from table2 a--给table2取别名a
where not exists(select 1 from table1 b --给table1取别名a
where a.row1=b.row1 and a.row2=b.row2)
/*
row1 row2
c x
d y
e z
*/
select * from tb2 a where not exists(
select 1 from tb1 b where a.row1=b.row1 and a.row2=b.row2)
select * from tb2 except select * from tb1
create table tb1(row1 char(1),row2 char(2))
create table tb2(row1 char(1),row2 char(2))
insert tb1
select 'a','o' union
select 'b','p' union
select 'c','q' union
select 'd','s'
insert tb2
select 'a','o' union
select 'b','p' union
select 'c','x' union
select 'd','y' union
select 'e','z'
select * from tb2 except select * from tb1
/*
row1 row2
c x
d y
e z */
select * from tb2 except select * from tb1
select * from table1 a inner join table2 b on a.row1 = b.row1 and a.row2<>b.row2