二手热处理网带炉:sql语句查重复

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/25 14:42:23
有数据表‘table’表内有安段name id
在name内有很多重复的数据,请问如何写sql直接将所有重复数据过滤掉只留一条
不是整个表内只留一条数据,而是我想要表内不能有重复数据

Select distinct name from table
or
Select min(id) as id,name from talbe group by name order by id desc

用如下语句删除

delete from [table]
where id not in(select max(id) from [table] group by name where id is not null)

第一步:
查询出所有的重复记录,根据name id字段:
select * from 表名
where 字段名 in (select 字段名 from 表名
group by 字段名 having count(字段名) > 1)
第二步:
删除表中多余的重复记录
第一步:select distinct * into #临时表名 from 数据源表
第二步:delete from 数据源表 --删除数据源表中的记录
第三步:insert into 数据源表 from #临时表
这样你的表中的记录全部是唯一的了,没有重复的记录。

用不重复的字段过滤掉

SELECT DISTINCT name FROM table
可以将表中所有不重复的 name 字段显示出来