删除单列和多列同时重复值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除单列和多列同时重复值相关的知识,希望对你有一定的参考价值。
在生产环境中,遇到前端应用未能限制,导致单列出现重复值,亦或是多列同时重复这种情况,这两种情况都是不允许的,现在由于前端应用不好限制,要做删除操作后,添加唯一索引,从数据库层面进行限制,以下是处理过程:
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)
本文出自 “岁伏” 博客,请务必保留此出处http://suifu.blog.51cto.com/9167728/1796055
以上是关于删除单列和多列同时重复值的主要内容,如果未能解决你的问题,请参考以下文章