sql server 怎么删除表里重复数据
2个回答
展开全部
1、必须保证表中有主键或者唯一索引,或者某列数据不能重复。只有这样,才可能使用一句SQL来实现。否则只能考虑其它办法。下面的语句,假定BB列是不重复的,删除后保存BB列值最大的那条记录。
delete
from
表
where
aa
in
(select
aa
from
表
group
by
aa
having
count(aa)
>
1)
and
bb
not
in
(select
max(bb)
from
表
group
by
aa
having
count(aa)
>
1);
2、有多种写法:
delete
A
from
B
where
A.AA
=
B.AA
delete
A
from
A,B
where
A.AA
=
B.AA
delete
A
where
AA
in
(select
AA
from
B)
3、使用into关键字:
select
*
into
新表名
from
原表
4、取数据前3位,字段必须是类似char类型,使用类似substring这样的函数(SYBASE是substring,ORACLE是substr):
select
substring(字段,1,3)
from
表名
delete
from
表
where
aa
in
(select
aa
from
表
group
by
aa
having
count(aa)
>
1)
and
bb
not
in
(select
max(bb)
from
表
group
by
aa
having
count(aa)
>
1);
2、有多种写法:
delete
A
from
B
where
A.AA
=
B.AA
delete
A
from
A,B
where
A.AA
=
B.AA
delete
A
where
AA
in
(select
AA
from
B)
3、使用into关键字:
select
*
into
新表名
from
原表
4、取数据前3位,字段必须是类似char类型,使用类似substring这样的函数(SYBASE是substring,ORACLE是substr):
select
substring(字段,1,3)
from
表名
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询