表结构
1 CREATE TABLE T_VENDOR ( 2 ID BIGINT(12) NOT NULL AUTO_INCREMENT COMMENT ‘ID‘, 3 COUNTRY CHAR(2) DEFAULT NULL COMMENT ‘国家‘, 4 VENDOR VARCHAR(20) DEFAULT NULL COMMENT ‘客户‘, 5 CAT_ID BIGINT(12) DEFAULT NULL COMMENT ‘分类ID‘, 6 APP_ID BIGINT(12) DEFAULT NULL COMMENT ‘APPID‘, 7 ORDBY BIGINT(12) DEFAULT NULL COMMENT ‘分类排序(在具体分类下的顺序)‘, 8 STATE CHAR(2) DEFAULT NULL COMMENT ‘分类状态(01 启用, 00禁用)‘, 9 CREATE_TIME DATETIME DEFAULT NULL COMMENT ‘创建日期‘, 10 UPDATE_TIME DATETIME DEFAULT NULL COMMENT ‘修改时间‘, 11 PRIMARY KEY (ID), 12 ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
实现目标
删除同一个国家、客户和分类下的重复APP,保留ID最小的记录
处理思路
Mysql不能把当前表的查询作为当前表的修改条件,故做一个二级子查询
删除脚本
DELETE FROM T_VENDOR WHERE ID NOT IN (SELECT MINID FROM (SELECT MIN(ID) AS MINID FROM T_VENDOR GROUP BY COUNTRY,VENDOR,CAT_ID,APP_ID) B);
原文:http://www.cnblogs.com/banning/p/6254169.html