select min(id),min(name),other fields....
from table
group by id,name

解决方案 »

  1.   

    sorry,现在的问题是:一个表中有两行数据除了id项和name项(自定义的项)show(自定义的项)不同外,其余都相同,我要将这两行合并为一行,怎么办?而且:如果其余项相同,就淘汰掉 name="system"的项
      

  2.   

    可以用一个临时表先将需要的记录保存起来。insert temp_table (id ,name ,other fields....)
     select min(id),min(name),other fields....
    from table
    group by id,name 然后delete temp_table最后将需要的数据倒回来就可以了
      

  3.   

    richincsdn2:舉個例子好嗎?
    並且說出你的DBMS
      

  4.   

    to pxq(风轻轻地吹)
    如果其余项相同,就淘汰掉 name="system"的项 啊!!!!!!!!
      

  5.   

    1、可以用程序段吗?
    2、是不是重复情况下,一定会有一个记录的NAME是SYSTEM?
      

  6.   

    表log:id    |   name           |   path                      |  show |1       c:\win.exe           c:\1234.exe                     02       system               c:\1234.exe                     13       c:\hello.exe         c:\test.exe                     14       d:\super.exe         c:\w32.exe                      05       system               c:\test.exe                     16       system               c:\w32.exe                      17       system               d:\rr.exe                       1我要把这个表变成这样
    id    |  name           |   Path                       | show  |1       c:\win.exe          c:\1234.exe                      03       c:\hello.exe         c:\test.exe                     14       d:\super.exe         c:\w32.exe                      07       system               d:\rr.exe                       1谢谢
      

  7.   

    to crope()  如果有一个 xxx.exe 就一定会有一个 system 与其相对应,这时侯 system 的id 一定会比 xxx.exe 大,但有一个system不一定有一个 xxx.exe 与其相对应,谢谢 
      

  8.   

    这条记录为什么会选出来?
    7      system              d:\rr.exe                      1
      

  9.   

    to icevi(按钮工厂)  因为有一个system不一定有一个 xxx.exe 与其相对应
      

  10.   

    是不是这个意思?
    select * 
    from log ,
     (select path,show from log where name="system") t1
    where log.path=t1.path and log.show=t1.show and log.name<>"system"
      

  11.   

    小改一下:
    select log.id,log.name,log.path,log.show 
    from log ,
    (select path,show from log where name="system") t1
    where log.path=t1.path and log.show=t1.show and log.name<>"system" 
      

  12.   

    如果id唯一的话可以
    select a.* from log a where exists(select b.id1 from 
    (select min(id) id1 from log group by path) b where a.id=b.id)
      

  13.   

    错了,应该是:
    select a.* from log a where exists(select b.id1 from 
    (select min(id) id1 from log group by path) b where a.id=b.id1) 
      

  14.   

    刚才错了。
    先添加一个标识字段 flag
    alter table table_name add flag int not null default(0)--先将只有一条的记录标识起来。
    update table_name set flag = 1 from 
                    ( select min(ID) as minID , count(ID) as num from table_name  group by other fields ) tt
                 where tt.num = 1 and ID = tt.minID--然后删除有两条重复信息中name= 'system'的记录delete table_name where flag  = 0 and name = 'system'最后删除flag字段
      

  15.   

    那我能不能这么认为:只要是有超过一条相同的纪录,就删除多余的?如果NAME=SYSTEM则有限删除?
      

  16.   

    to xzou(亡狼补齿)   老哥 那个 a 是什么啊
      

  17.   

    to pxq(风轻轻地吹) 
       
       步骤太多啦些,不直接
      

  18.   

    xzou(亡狼补齿) 的思路很好啊,id是唯一的,可是你的语句我看不太东
      

  19.   

    delete
      from table1 as x
     where path + "-" + show in (select path + "-" + show from table1
                                                  group by path, show
                                                 having count(*) > 1)
        and name = "system";
      

  20.   

    很简单,
    (select min(id) id1 from log group by path) b 表示取path相同的最小的id的集合,因为如果path如有重复,取小的id符合条件“如果有一个 xxx.exe 就一定会有一个 system 与其相对应,这时侯 system 的id 一定会比 xxx.exe 大”,如果path没重复,max(id)就是直接取唯一的id,而你需要的纪录集的每一个id都在我的b表里,你不需要的id都不再我的b表里,这样就很容易取的log表id在b表里就取出来,这就是最终答案了。没有测试,不知道结果如何,思路反正就是这样。
      

  21.   

    写成这样不知道是不是清楚一点
    select a.* from log a where a.id in(select b.id1 from 
    (select min(id) id1 from log group by path) b); 
      

  22.   

    如果system 的id 一定会比 xxx.exe 大,那我们可以group by other fields 取小的ID
    那么这就是我们要保留的记录。
    delete from table_name  
                where ( table_name.name = 'system' 
                        and 
                        not table_name.ID  in (select min(ID) as minID as num from table_name  group by other fields )
                      )
      

  23.   

    改进:
    delete
      from table1 as x
     where path + "-" + show in (select path + "-" + show from table1
                                                  group by path, show
                                                 having count(*) > 1)
        and id not in (select min(id) from table1 as y
                          where y.path = x.path
                              and y.show = x.show
                              and y.name <> "system");
      

  24.   

    刚才有点错误delete from table_name  
                where ( table_name.name = 'system' 
                        and 
                        not table_name.ID  in (select min(ID) from table_name  group by other fields )
                      )
      

  25.   

    哈哈哈哈,综合大家的意见,各位看看这个对不对
    delete from table_name  
                where   table_name.ID not in (select min(ID) from table_name  group by other fields )