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

insert into 怎么不重复数据

2012-01-24 
insert into 如何不重复数据现有两表表amid门店1aa2bb3cc4dd5ee6ff7gg8hh表bmid门店1aa2bb3cc7gg8hh9ii10j

insert into 如何不重复数据
现有两表
表a
  mid 门店
1aa
2bb
3cc
4dd
5ee
6ff
7gg
8hh

表b
  mid 门店
1aa
2bb
3cc
7gg
8hh
9ii
10jj
11kk
现从表b向表a添加数据.请问如何不会重复添加
想要的结果:
表a
  mid 门店
1aa
2bb
3cc
4dd
5ee
6ff
7gg
8hh
9ii
10jj
11kk


[解决办法]

SQL code
--tryinsert into a select * from b where mid not in (select mid from a)
[解决办法]
SQL code
--> 测试数据: #aif object_id('tempdb.dbo.#a') is not null drop table #acreate table #a (mid int,门店 varchar(2))insert into #aselect 1,'aa' union allselect 2,'bb' union allselect 3,'cc' union allselect 4,'dd' union allselect 5,'ee' union allselect 6,'ff' union allselect 7,'gg' union allselect 8,'hh'--> 测试数据: #bif object_id('tempdb.dbo.#b') is not null drop table #bcreate table #b (mid int,门店 varchar(2))insert into #bselect 1,'aa' union allselect 2,'bb' union allselect 3,'cc' union allselect 7,'gg' union allselect 8,'hh' union allselect 9,'ii' union allselect 10,'jj' union allselect 11,'kk';-->如果门店不重复insert into #a select * from #b where not exists (select 1 from #a where 门店=#b.门店)select * from #a/*mid         门店----------- ----1           aa2           bb3           cc4           dd5           ee6           ff7           gg8           hh9           ii10          jj11          kk*/ 

热点排行