怎么删除多余的数据?
表A
id name
1 a
2 a
3 c
4 c
5 c
6 d
怎么删除,以变成
id name
1 a
3 c
6 d
谢谢!
问题点数:0、回复次数:6Top
1 楼sdhdy(大江东去...)回复于 2003-12-01 15:54:19 得分 0
delete a where id not in (select min(id) from a group by name)Top
2 楼wzh1215(懒猫)回复于 2003-12-01 15:56:37 得分 0
delete from 表A
where id not in (select min(id) from 表A group by name)Top
3 楼CrazyFor(冬眠的鼹鼠)回复于 2003-12-01 15:57:13 得分 0
如果有ID字段,就是具有唯一性的字段
delect table where id not in (
select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
2,如果是判断所有字段也可以这样
select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa
3,没有ID的情况
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
col1+','+col2+','...col5 联合主键
select * from table where col1+','+col2+','...col5 in (
select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
2,
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
Top
4 楼lvltt(未完成)回复于 2003-12-01 16:12:08 得分 0
delete a from 表A where id not in (select min(id) from a group by name)
Top
5 楼liuxiaoyuzhou(蟀哥)回复于 2003-12-01 18:17:50 得分 0
select distinct name ,id
form表名
解决!Top
6 楼dlpseeyou(豆子)回复于 2003-12-01 18:21:16 得分 0
select distinct(name) ,id form 表名
Top




