+ -
当前位置:首页 → 问答吧 → 如何查找相同数据

如何查找相同数据

时间:2011-11-21

来源:互联网

在sql中 我有两个表 一个4000多数据 一个6000多条数据 这两个表内容一样 那个4000多是从6000多条数据里拿出来的 ,
现在我想联合查询 这两个表不同的数据 然后提出来 放在另一个表中 。各位怎么写啊?

作者: seven4634   发布时间: 2011-11-21

SQL code
insert newtable
select a.*
from tab6000 a left join tab4000
on a.key = b.key
where b.key is null

作者: Haiwer   发布时间: 2011-11-21

select * from table_a where id not in (select id from table_b)

作者: jyh070207   发布时间: 2011-11-21

or :

SQL code
insert newtable
select a.*
from tab6000 a 
where not exists (
select 1
from tab4000 b
on a.key = b.key
)

作者: Haiwer   发布时间: 2011-11-21

修正3楼

SQL code
insert newtable
select a.*
from tab6000 a 
where not exists (
select 1
from tab4000 b
where a.key = b.key
)

作者: Haiwer   发布时间: 2011-11-21

select A.* from A where A.ID not in (select B.ID From B)
A:6000多条数据表
B:4000多条数据表
ID:为主键
这样就可以提取出所有的不同数据插入到另一个表中了

作者: qiuyu820968   发布时间: 2011-11-21

SQL code

--A表是6000多条数据表
--B表是4000多条数据表
--C表是新生成的表
insert into C
select * from A t1 where not exists(select 1 from B where id=t1.id)

作者: pengxuan   发布时间: 2011-11-21