删除单列和多列同时重复值

在生产环境中,遇到前端应用未能限制,导致单列出现重复值,亦或是多列同时重复这种情况,这两种情况都是不允许的,现在由于前端应用不好限制,要做删除操作后,添加唯一索引,从数据库层面进行限制,以下是处理过程:

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)

时间: 2024-10-14 00:59:23

删除单列和多列同时重复值的相关文章

oracle 查某一列有重复值的记录

-- 查找重复记录select names,num from test where rowid != (select max(rowid)                  from test b                 where b.names = test.names and                      b.num = test.num) 或者使用 select names,num from test where rownum!= (select max(rownum

SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单纯的查询 GX 这一列用 distinct 是没有任何问题的 1 select distinct t.GX from [PeopleCount] as t where t.pr

Sql server 多列去重复值,相同的只显示一条数据

CREATE TABLE #tp( headerNo VARCHAR(10), machineNO VARCHAR(10), descrption nVARCHAR(20), artNo VARCHAR(20), qty INT , repartno varchar(20) , repqty INT) insert INTO #tp SELECT 'HD01','0101520',N'电池出问题','102020',2,'102020',2insert INTO #tp SELECT 'HD01

innodb 自增列重复值问题

1 innodb 自增列出现重复值的问题 先从问题入手,重现下这个bug use test; drop table t1; create table t1(id int auto_increment, a int, primary key (id)) engine=innodb; insert into t1 values (1,2);insert into t1 values (null,2); insert into t1 values (null,2); select * from t1;

去除列中的重复值

CSV中原内容: 要求去除A列中的重复字段,去除后如下: 代码如下: 1 $dd = import-csv d:\disks.csv 2 #通过倒序方法去除列中重复值 3 for ($i=$dd.count-1;$i -ge 0;$i--) 4 { 5 $previous = $dd[$i-1].Server 6 $current = $dd[$i].Server 7 If ($current -eq $previous) 8 { 9 $dd[$i].Server = "" 10 }

删除DataTable重复列,只针对删除其中的一列重复的行

vs2005针对datatable已经有封装好的去重复方法: 1 //去掉重复行 2 DataView dv = table.DefaultView; 3 table = dv.ToTable(true, new string[] { "name", "code" }); 4 5 此时table 就只有name.code无重复的两行了,如果还需要id值则 6 7 table = dv.ToTable(true, new string[] { "id&quo

返回数组中的最大值和删除数组重复值-排序

//数组中最大值function getMax(arr){ //取该数组第一个值为最大值 var max=arr[0]; for(var i=0;i<arr.length;i++){ if(arr[i]>max){ max=arr[i] } } return max;} console.log(getMax([2,98,10,88])) /*--------------------------------------------------------------------*///删除数组重

Mysql查询某字段重复值并删除重复值

1.查询重复值: select code,count(*) as count from hospital group by code having count>1; 该语句查询code重复值大于1的记录 2.删除重复: DELETE FROM hospital WHERE id NOT IN (SELECT dt.minno FROM (SELECT MIN(id) AS minno FROM hospital GROUP BY code) dt); 该语句保留id最小的记录,其余code重复的

sql查询去除重复值语句

sql 单表/多表查询去除重复记录 单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 ************************************************************************************ 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select