1

我有重复的结果,如下所示,其中某些列可能有数据,可能没有

| contact_info | icon | id  | title         | lastmodified_by  |
+--------------+------+-----+---------------+------------------+
|          169 |  305 | 123 | Whakarewarewa | 2011100400305262 |
|         NULL | NULL | 850 | Whakarewarewa | NULL             |
+--------------+------+-----+---------------+----------------



| contact_info | icon | id  | title         | lastmodified_by  |
+--------------+------+-----+---------------+------------------+
|         NULL | NULL | 123 | Paris         | NULL             |
|         NULL | NULL | 850 | Paris         | NULL             |
+--------------+------+-----+---------------+----------------

我想删除数据较少的记录,如果所有字段值完全相同,则删除任何行。有上千条这样的记录。

4

3 回答 3

3

试试这个两步解决方案:

运行此查询以查看所有重复项 -记录具有较少数据-

SELECT t1.* FROM table t1
  JOIN (
    SELECT
      title,
      MIN(IF(contact_info IS NULL, 0, 1) + IF(contact_info IS NULL, 0, 1) + IF(lastmodified_by IS NULL, 0, 1)) min_value_data,
      MAX(IF(contact_info IS NULL, 0, 1) + IF(contact_info IS NULL, 0, 1) + IF(lastmodified_by IS NULL, 0, 1)) max_value_data
    FROM table GROUP BY title HAVING min_value_data <> max_value_data
  ) t2
  ON t1.title = t2.title AND IF(t1.contact_info IS NULL, 0, 1) + IF(t1.contact_info IS NULL, 0, 1) + IF(t1.lastmodified_by IS NULL, 0, 1) <> t2.max_value_data

将其重写为 DELETE 语句并执行。


然后运行此查询以删除除最小 ID 之外的所有重复项:

DELETE t1 FROM table t1
  JOIN (SELECT MIN(id) id, title FROM table GROUP BY title) t2
    ON t1.id <> t2.id AND t1.title = t2.title;
于 2012-06-05T08:33:38.647 回答
1

使用它来选择重复项,随意将其更改为删除语句:

SELECT * FROM `test`,
(SELECT title, count( title ) AS ttl
FROM `test`
GROUP BY title
HAVING ttl >1) AS sub

WHERE test.title = sub.title
AND contact_info IS NULL AND lastmodified_by IS NULL 
于 2012-06-05T08:55:35.557 回答
0

主表 = tes1

创建温度

创建临时表 my_temp ( id INT(20) NOT NULL ) ENGINE=MEMORY;

填写要删除的id

INSERT INTO my_temp (id) SELECT id FROM tes1 AS main, (SELECT title, count(title) AS ttl FROM tes1 GROUP BY title HAVING ttl >1 ) AS sub WHERE main.title = sub.title AND main.contact_info 为 NULL AND main.lastmodified_by IS NULL GROUP BY main.contact_info、main.icon、main.title、main.lastmodified_by;

删除!

DELETE FROM tes1 WHERE id IN(从 my_temp 中选择 id);

清理,注意:我们真的需要这个吗?

删除表 my_temp;

于 2012-06-05T11:14:35.753 回答