Skip to content

Instantly share code, notes, and snippets.

@hfhimage
Created January 24, 2014 07:09
Show Gist options
  • Save hfhimage/8593316 to your computer and use it in GitHub Desktop.
Save hfhimage/8593316 to your computer and use it in GitHub Desktop.
mysql 删除重复数据 mysql 4 不支持嵌套的删除语法, 因此得需要通过创建临时表绕过
DROP TABLE IF EXISTS tmp_dels;
CREATE TABLE tmp_dels AS SELECT subscribe_tags.id FROM subscribe_tags, ( SELECT max(id) AS id, nick, tag_id FROM subscribe_tags GROUP BY nick, tag_id HAVING count(1) > 1 ) t WHERE subscribe_tags.nick = t.nick AND subscribe_tags.tag_id = t.tag_id AND subscribe_tags.id < t.id;
DELETE FROM subscribe_tags WHERE id IN (SELECT id FROM tmp_dels);
DROP TABLE IF EXISTS tmp_dels;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment