---一道SQL题目,向所有CSDN高手请教-------------

cefriend 2008-02-27 02:38:12
求一SQL语句实现数据表之间的去重复项

如有一表AT 中有四个字段

a b c d 其中 a字段,b字段,c字段为联合主键
数据如下

a b c d
1 11 22 aa
1 22 22 bb
1 33 22 cc
2 11 22 dd

有另一张表AT_Temp临时表
也有四个个字段

a b c d 其中 a字段,b字段,c字段为联合主键
数据如下
a b c d
1 22 22 bb
1 22 33 ff
2 11 44 gg

请问如何实现AT_Temp表中与AT表去掉重复记录并将不重复数据插入到AT表中

我如道如果只有一个主键 a 的话用

insert into AT select * from A_Temp from a not in (select a from AT) 可以做出来,但当他有三个主键
后我在后面中 and b not in (select b from AT) ... 是不可行的,并且效率低,求一好的SQL
...全文
113 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2008-02-27
  • 打赏
  • 举报
回复
匆忙中漏写表,现在才发现.多谢海阔天空.

select at_temp.* from at_temp where not exists(select 1 from at where at.a = at_temp.a and at.b = at_temp.b and at.c = at_temp.c)
zhuyx808 2008-02-27
  • 打赏
  • 举报
回复
你们写语句,我来接个分
firefox_1983 2008-02-27
  • 打赏
  • 举报
回复
晕,居然贴错地方了。
firefox_1983 2008-02-27
  • 打赏
  • 举报
回复
SELECT 
name,
classid,
counts,
classname
FROM
merchant
WHERE
counts in
(
SELECT
max(counts)
FROM
merchant
GROUP BY
classname
)
ORDER BY
counts asc
gahade 2008-02-27
  • 打赏
  • 举报
回复

insert into AT
select * from A_Temp from a
where not exists(select 1 from at where a.a=at.a and a.b=at.b and a.c=at.c)
昵称被占用了 2008-02-27
  • 打赏
  • 举报
回复
修正2楼的

select at_temp.* from at_temp where not exists(select 1 from at where at_temp.a = at.a and at_temp.b = at.b and at_temp.c = at.c)
昵称被占用了 2008-02-27
  • 打赏
  • 举报
回复
修正一楼的

insert   into   AT   
select * from A_Temp a
where not exists(select 1 from at b where a.a=b.a and a.b=b.b and a.c=b.c)
昵称被占用了 2008-02-27
  • 打赏
  • 举报
回复
insert   into   AT   
select * from A_Temp from a
where not exists(select 1 from at b where a.a=b.a and a.b=b.b and a.c=b.c)


dawugui 2008-02-27
  • 打赏
  • 举报
回复
select at_temp.* from at_temp where not exists(select 1 where at_temp.a = at.a and at_temp.b = at.b and at_temp.c = at.c)
gahade 2008-02-27
  • 打赏
  • 举报
回复

insert into AT
select * from A_Temp from a
where not exists(select 1 from at b where a.a=at.a and a.b=at.b and a.c=at.c)

34,594

社区成员

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

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