假设有一表为tabel1,包括如下字段:z1,z2,z3,z4,z5,z6,z7,z8  现在要删除z4,z5,z6 这三字段完全相同的记录,但删除时要保留这些记录中z1最大的记录和z8最小的记录。如何用SQL 实现?

解决方案 »

  1.   

    我也想知道,up for you and me
      

  2.   

    采用临时表存储最大与最小值,将其TABLE1中的相同记录全部删除,然后再将临时表的数据写回去,就OK了,我想SQL语句不用我教你吧!
      

  3.   

    delete from table1 
    where z4 & z5 & z6 in (select z4 & z4 & z6 form table1 where count(z4 & z5 & z6) > 1) 
    and z1 not in (select max(z1) from table1) 
    and z8 not in (select min(z8) from table1)
      

  4.   

    用kj_wang 的方法应该可行,但比较繁琐,可有更好的方法?
    用of123 的方法总是提示: “where 子句中不能有合计函数”. 不知何故?
      

  5.   

    delete from table1 
    where id in (select id form table1 group by z4 & z5 & z6 ) 
    and z1 not in (select max(z1) from table1) 
    and z8 not in (select min(z8) from table1)