表结构 (marcidx)
id f1 f2
001 010a 7-5053-7707-8
001 200a vb6.0程序设计
001 210c 吉林人民出版社
002 010a 7-5053-7707-8
002 200a delphi7教程
002 690a tp312
........................
001 代表一本书的信息 记录
002 是另一本书
现在我要向这个表 导入数据 导入之前要查重也就是说 重复的图书我就不导入了
这里我自定义查重条件 010a,200a也就是说 当且仅当字段 f1 010a 200a 都与将要导入的数据相同时 认为是重复
比如导入的记录内容 : 010a=7-5053-7707-8 200a =vb6.0程序设计 就会发现数据库已经存在001 这样的图书记录了 不再导入 而导入 010a=7-5053-7707-8 200a =c#教程 则可以导入 不认为是重复。
------解决方案--------------------
if exists (
select 1 from marcidx a where a.f1= '010a' and a.f2 = '7-5053-7707-8'
and exists (
select 1 from marcidx b where b.f1= '200a' and b.f2 = 'vb6.0程序设计'
and b.id = a.id
)
)
begin
insert marcidx(id,f1,f2) values('001','010a','7-5053-7707-8')
insert marcidx(id,f1,f2) values('001','200a','vb6.0程序设计')
insert marcidx(id,f1,f2) values('001','200a','吉林人民出版社')
end
------解决方案--------------------
010a=7-5053-7707-8 200a =c#教程
010a=7-5053-7707-8 200a =vb6.0程序设计
ISBN重復,不可能吧?
------解决方案--------------------
如果是这样,那就:
insert into marcidx(id ,f1,f2)
select * from(
select '003' id,'010a' f1,'7-5053-7707-8' f2
union
select '003','200a','vb6.0程序设计'
union
select '003','210c','XX出版社'
)t where not exists(select 1 from marcidx where f2='7-5053-7707-8')
and not exists(select 1 from marcidx where f2='vb6.0程序设计')
--如果只要用id 来比较的话,那只要写一句 where not exists(select 1 from marcidx where id='003')
------解决方案--------------------
where not exists(select 1 from marcidx where f2='7-5053-7707-8')
and not exists(select 1 from marcidx where f2='vb6.0程序设计')
这个判断与你的逻辑要求是不同的,因为这个逻辑,只要其他属性的f2='7-5053-7707-8'或者f2='vb6.0程序设计'就不添加了,根本不是你要的逻辑
------解决方案--------------------
3个一样的哦
if not exists (
select 1 from marcidx a where a.f1= '010a' and a.f2 = '7-5053-7707-8'
and exists (
select 1 from marcidx b where b.f1= '200a' and b.f2 = 'vb6.0程序设计'
and b.id = a.id
)
and exists (
select 1 from marcidx b where b.f1= '690a' and b.f2 = 'xxxxxxxx'
and b.id = a.id
)
)
begin
insert marcidx(id,f1,f2) values('001','010a','7-5053-7707-8')
insert marcidx(id,f1,f2) values('001','200a','vb6.0程序设计')
insert marcidx(id,f1,f2) values('001','690a','xxxxxxx')
end
------解决方案--------------------
为什么不先把这个表结构改一下就好判断了。
id f1 f2
001 010a 7-5053-7707-8
001 200a vb6.0程序设计
001 210c 吉林人民出版社
002 010a 7-5053-7707-8
002 200a delphi7教程
002 690a tp312
改为
id f1 f2,f1_1,f1_2,
001 010a 7-5053-7707-8 200a vb6.0程序设计
002 010a 7-5053-7707-8 200a delphi7教程
这样一行就好比较了。