DELIMITER //
CREATE PROCEDURE del_proc()
BEGIN
/*创建临时表*/
CREATE TEMPORARY TABLE IF NOT EXISTS tmpTable(
`id` INT(11) NOT NULL,
`old_id` INT(11) NOT NULL
)ENGINE =MEMORY CHARSET=utf8 COMMENT '临时表用于去重';
#将deltable中重复的数据取出一条放入临时表中
INSERT INTO tmpTable(id,old_id) SELECT id, old_id FROM deltable GROUP BY old_id HAVING COUNT(old_id)>1;
#删除deltable中的但不在tmpTable中的数据,条件是id不等,old_id相等
SELECT COUNT(*) FROM tmpTable;
#删除deltable中重复的数据
DELETE FROM deltable USING deltable, tmpTable WHERE tmpTable.old_id = deltable.old_id AND deltable.id != tmpTable.id;
#清空临时表
DELETE FROM tmpTable WHERE 1=1;
END;//
DELIMITER ;
阅读(1818) | 评论(0) | 转发(0) |