表中只有一个字段,把重复的记录删掉。比如一共有一千行记录,其中有好些记录是重复的,那么现在就需要删掉多余的,只剩一个就行。就是让每一条记录都是唯一的。怎么写代码?

解决方案 »

  1.   

    select distinct 一个字段 from 表;先将上述结果存入另一临时表,然后将原表清空后再半临时表记录取回。
      

  2.   

    mysql> create table test1(a int);
    Query OK, 0 rows affected (0.20 sec)mysql> insert into test1 values(1),(1),(2),(2);
    Query OK, 4 rows affected (0.06 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql> create table test2 select distinct a from test1;
    Query OK, 2 rows affected (0.13 sec)
    Records: 2  Duplicates: 0  Warnings: 0mysql> truncate table test1;
    Query OK, 0 rows affected (0.05 sec)mysql> insert into test1 select a from test2;
    Query OK, 2 rows affected (0.03 sec)
    Records: 2  Duplicates: 0  Warnings: 0