批量插入不重复记录,在线等

idcfly 2008-07-17 04:44:36
表中有:
ID newsid a b
1 240 4 1
2 240 5 1
3 240 6 2
4 241 3 1
5 241 4 1


对于来源同一newsid , a不复复。
我现在批量插入二条记录
newsid=240 ,a=4
newsid=240 ,a=6

第一条记录,有重复,则不插入;
只插入第二条。
提交时,newsid,a值来源于表单下拉列表。
...全文
270 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wzy_love_sly 的回复:]
结帖吧 今才得200多分..
[/Quote]

200多分还少吗?
idcfly 2008-07-17
  • 打赏
  • 举报
回复
感谢楼上教导。
wzy_love_sly 2008-07-17
  • 打赏
  • 举报
回复
结帖吧 今才得200多分..
wzy_love_sly 2008-07-17
  • 打赏
  • 举报
回复
就是把你的几个值用as 变成表,然后条件是表的数据不能在 你插入的t_tjly里有这条记录
条件是newsid=t.newsid and news_tjto=t.news_tjto
t.newsid是240,t.news_tjto是4,因为t里就一条记录
insert into t_tjly(newsid,news_tjto) select * from (
select '240' as newsid,'4' as news_tjto
union all
select '246' as newsid,'4' as news_tjto
union all
select '248' as newsid,'4' as news_tjto

) t where not exists( select 1 from t_tjly where newsid=t.newsid and news_tjto=t.news_tjto)


也可以用程序连起来,连成select ... union all selec t... union all select ...,

执行一次就行,存在的不会重复插入
idcfly 2008-07-17
  • 打赏
  • 举报
回复
我刚刚测试了。wzy_love_sly 的应该可以。
烦解释一下好吗,谢谢。我半懂不懂。
wzy_love_sly 2008-07-17
  • 打赏
  • 举报
回复
insert into t_tjly(newsid,news_tjto)
select * from (select '240' as newsid,'4' as news_tjto) t where not exists(
select 1 from t_tjly where newsid=t.newsid and news_tjto=t.news_tjto)


如果拼语句,把240和4换成变量就行
idcfly 2008-07-17
  • 打赏
  • 举报
回复
哦,写错了
insert into t_tjly (newsid,news_tjto) values(240,7) where news_tjto not in(select news_tjto from t_tjly where newsid=240 )

idcfly 2008-07-17
  • 打赏
  • 举报
回复
相当于这个意思
240,4
240,7

从表单提交过来。

insert into t_tjly (newsid,news_tjto) values(240,4) where news_tjto not in(select news_tjto from t_tjly where newsid=239 )

insert into t_tjly (newsid,news_tjto) values(240,7) where news_tjto not in(select news_tjto from t_tjly where newsid=239 )

实际上我写错了。怎么改应该?
wzy_love_sly 2008-07-17
  • 打赏
  • 举报
回复
那得先查一下有没有这数据吧
或用下面这种方式

create table tb(id int,name varchar(50))
insert into tb select 1,'a'
insert into tb select 2,'b'
insert into tb select 3,'c'

insert into tb(id,name)
select * from (select 1 as id ,'a' as name) t where not exists(
select 1 from tb where id=1 and name='a')

select * from tb


idcfly 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 roy_88 的回复:]
SQL codeinsert 插入表
select * from newsid where checksum(newsid,a)not in(select checksum(newsid,a) from 插入表)
[/Quote]

仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 't_tjly' 中为标识列指定显式值。
idcfly 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wzy_love_sly 的回复:]
数据从另一个表来:


SQL codeinsert into [你要插入的表名](id,newsid,a,b)
select id,newsid,a,b from [数据来源表] t where id not in(
select id from [你要插入的表名]
)
[/Quote]


没有数据来源表,只是从表单提交过来的数据。下拉列表过来的。
idcfly 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lgxyz 的回复:]
什么意思
newsid=240 ,a=6 不是也重复?
[/Quote]

我写错了。
newsid=240 ,a=7 吧。

反正我的意思,你们理解吧。

我来试下大家方法哦
ojuju10 2008-07-17
  • 打赏
  • 举报
回复




ID newsid a b
1 240 4 1
2 240 5 1
3 240 6 2
4 241 3 1
5 241 4 1


insert into tb(newsid,a,b)
select newsid,a,b from ta a
where not exists(select 1 from tb b where a.newsid=b.newsid and a.a=b.a)

中国风 2008-07-17
  • 打赏
  • 举报
回复
insert 插入表
select * from newsid where checksum(newsid,a)not in(select checksum(newsid,a) from 插入表)
lgxyz 2008-07-17
  • 打赏
  • 举报
回复
什么意思
newsid=240 ,a=6 不是也重复?
wzy_love_sly 2008-07-17
  • 打赏
  • 举报
回复
数据从另一个表来:

insert into [你要插入的表名](id,newsid,a,b)
select id,newsid,a,b from [数据来源表] t where id not in(
select id from [你要插入的表名]
)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧