删除一张表中重复记录的SQL语句怎么写?
例如: 
table T :
 +----+-------+
| id | name  |
+----+-------+
|  1 | fuck  |
|  2 | b     |
|  3 | c     |
|  4 | d     |
|  5 | hone  |
|  6 | ho'n  |
|  7 | ho'ne |
|  8 | zho"u |
|  9 | a     |
| 10 | a     |
| 11 | b     |
+----+-------+
如何删除表中name字段有重复的记录?在这个例子中'a'和'b'有重复,即在id为9和10的记录保留一条既可,id为2和11的记录保留一条既可.
这个SQL语句怎么写?要求用一条语句。
thanks very much!

解决方案 »

  1.   

    select name,min(id) from tt group by name
      

  2.   

    自己顶一下.查出来后怎么删?怎么在一个sql 语句中完成?
      

  3.   

    直接生成一个新表是最简单的方法
    select name,min(id) into newtt from tt group by name 
      

  4.   

    create table t 
    (
    id int,
    name varchar(10)
    )
    insert into t(id,name)select 1,'fuck' union all
    select 2,'b'  union all
    select 3,'c' union all
    select 4,'d' union all 
    select 5,'hone' union all  
    select 6,'ho''n' union all  
    select 7,'ho''ne' union all
    select 8,'zhou' union all 
    select 9,'a' union all 
    select 10,'a' union all 
    select 11,'b'delete t from t inner join  
    (
    SELECT id from t where EXISTS (select 1 from t as tb where name=t.name and id <t.id)
    ) as tbr
    on t.id = tbr.id
    select * from t
    -- result 
    1 fuck
    2 b
    3 c
    4 d
    5 hone
    6 ho'n
    7 ho'ne
    8 zhou
    9 a
      

  5.   

    如果只用一条语句可以这样:
    delete T as a from T as a,
    (
    select *,min(id) from T group by name having count(*) > 1
    ) as b
     where a.name = b.name and a.id > b.id;
      

  6.   

     修改下:
    delete T as a from T as a, 

    select *,min(id) from T group by name having count(1) > 1 
    ) as b 
     where a.name = b.name and a.id > b.id;
      

  7.   


    delete from t where id not in (select distinct name from t) 
      

  8.   

    count(*) 和count(1)有什么区别?