在生产环境中,遇到前端应用未能限制,导致单列出现重复值,亦或是多列同时重复这种情况,这两种情况都是不允许的,现在由于前端应用不好限制,要做删除操作后,添加唯一索引,从数据库层面进行限制,以下是处理过程:
mysql> select * from aixuan1;
+----+------+-------+
| id | text | text1 |
+----+------+-------+
| 1 | aa | 11 |
| 2 | bb | 22 |
| 3 | cc | 33 |
| 4 | cc | 44 |
| 5 | bb | 22 |
| 6 | aa | 11 |
| 7 | dd | 55 |
+----+------+-------+
7 rows in set (0.00 sec)
text字段全部重复的有:
mysql> select * from aixuan1 where text in (select text from aixuan1 GROUP BY text having count(*) > 1);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
| 1 | aa | 11 |
| 2 | bb | 22 |
| 3 | cc | 33 |
| 4 | cc | 44 |
| 5 | bb | 22 |
| 6 | aa | 11 |
+----+------+-------+
6 rows in set (0.00 sec)
筛选出text单列重复值
select * from aixuan1 where `text` in (select `text` from aixuan1 GROUP BY `text` having count(*) > 1) and id not in (select min(id) from aixuan1 group by text having count(*)>1)
+----+------+-------+
| id | text | text1 |
+----+------+-------+
| 4 | cc | 44 |
| 5 | bb | 22 |
| 6 | aa | 11 |
+----+------+-------+
3 rows in set (0.00 sec)
还可以这么查
mysql> select * FROM aixuan1 WHERE id NOT IN ( SELECT temp.mid FROM ( SELECT min(id) as mid FROM aixuan1 em GROUP BY em.text) AS temp);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
| 4 | cc | 44 |
| 5 | bb | 22 |
| 6 | aa | 11 |
+----+------+-------+
3 rows in set (0.00 sec)
筛选出text和text1同时重复的字段:
mysql> select * FROM aixuan1 WHERE id NOT IN ( SELECT temp.mid FROM ( SELECT min(id) as mid FROM aixuan1 em GROUP BY em.text,em.text1) AS temp);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
| 5 | bb | 22 |
| 6 | aa | 11 |
+----+------+-------+
2 rows in set (0.00 sec)