友情提示,删除数据须谨慎,在运行下面代码前,先用一些模拟数据毕辩测试一下,不过下面SQL应租伏该没问题,弊数携^_^
delete from A where id not in (select max(id) from A group by 字段2)
1.先利用查询每个字段2含最大ID的行—袭握—查询1
SELECT Max(表A.ID) AS ID
FROM 表A
GROUP BY 表A.字段拍镇庆2;
2.然后利用查询1删除其他的行——查询旅毕2
DELETE 表A.*
FROM 表A
WHERE 表A.id not in (select id from 查询1);
当然也可以合并为一句
DELETE 表A.*
FROM 表A
WHERE 表A.id not in (SELECT Max(表A.ID) FROM 表A GROUP BY 表A.字段2);
delete a from a,(select 字段2,max(id) as id from a group by 字州拆段2) b where a.字和迹氏段唤散2=b.字段2 and a.id<>b.id