if object_id('[a]') is not null drop table [a]
go
create table [a]([tel_no] bigint,[cost] int)
insert [a]
select 13800000000,38 union all
select 13823400000,56 union all
select 13800056400,88 union all
select 13800230000,28 union all
select 13802300000,18 union all
select 13822220000,68 union all
select 13844400000,98 union all
select 13833330000,35 union all
select 13822220000,31 union all
select 13811110000,32
--> 测试数据:
if object_id('') is not null drop table
go
create table ([tel_no] bigint)
insert
select 13800000000 union all
select 13823400000 union all
select 13800051230 union all
select 13800230123
现在要查出两张表相同的数据和两张表不同的数据,如果在SQL SERVER 2005以上版本:
--相同数据
select tel_no
from a
intersect
select tel_no
from b
--不同数据
select tel_no
from b
except
select tel_no
from a
如果是SQL SERVER 2000
SELECT * FROM b WHERE EXISTS(SELECT 1 FROM a WHERE tel_no=b.tel_no)
SELECT * FROM b WHERE NOT EXISTS(SELECT 1 FROM a WHERE tel_no=b.tel_no)