创建了一个表名字是Table1,其中记录的重复类似如下:
a1, b1, c1, d1, 4
a1, b1, c12, d12, 5
a2, b2, c2, d2, 7
a3, b3, c3, d3, 15
a3, b3, c31 d31, 17...表的记录是这样的
1、前两列重复的,重复时,重复的记录只重复一次,比如,第1,2条记录,第3,4条记录;
2、也有不重复的记录,比如第3条记录;
3、现在我想删除重复的记录的一个,保留第5列最大的那个;上面的表删除后的效果是这样的
a1, b1, c12, d12, 5
a2, b2, c2, d2, 7
a3, b3, c31 d31, 17请问如何快速删除?
我用的是Mysql。
在此声明一下,本人sql语句不是很熟,回答是麻烦详细一点。谢谢了。

解决方案 »

  1.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    select *
    from tb A
    where not exists (select 1 from tb where A.col1=col1 and A.col2=col2 and A.col5<col5)
      

  3.   

    这样的速度太慢了。
    我是了一下这样,但是,不是我想要的结果,
    1. create table tmptable like Table1
    2. insert into tmptable select col1,col2,col3,col4,MAX(col5) from Table1 group by col1,col2 3. drop table Table1 

    4. rename tmptable shot_infotmp to Table1 
    这样的删除的记录col3,col4保存的是前一条记录的值。就上面的表来说,删除结果为,a1, b1, c1, d1, 5
    a2, b2, c2, d2, 7
    a3, b3, c3, d3, 17
      

  4.   

    select *
    from 
    (
    select * from Table1 order by col1,col2,col5 desc
    ) t
    group by col1,col2
    由于没有索引,并且记录平均分配,所以必然要出现全表扫描,速度不可能太快。
      

  5.   

    现在我对前两列col1,col2 建立索引 col1_col2_index;
    可以提高效率吗?
    且如何删除重复的记录呢?
      

  6.   


    [email protected]>alter table test add index idx_id_age_p(id,age,p);
    Query OK, 4 rows affected (0.03 sec)
    Records: 4  Duplicates: 0  Warnings: [email protected]>select id,age,max(p) from test group by id,age;
    +------+------+--------+
    | id   | age  | max(p) |
    +------+------+--------+
    |    1 |    1 |      2 | 
    |    2 |    2 |      4 | 
    +------+------+--------+
    2 rows in set (0.02 sec)[email protected]>delete from test where (id,age,p) not in (select * from (select id,age,max(p) as p from test group by id,age) as tmp);
    Query OK, 2 rows affected (0.02 sec)[email protected]>select * from test;
    +------+------+------+
    | id   | age  | p    |
    +------+------+------+
    |    1 |    1 |    2 | 
    |    2 |    2 |    4 | 
    +------+------+------+
    2 rows in set (0.00 sec)