删除完全一样的重复行
现场表结构没有设置主键,导致数据库插入数据存在多个完全相同的记录,需要删除完全相同数据。
MySQL
方案一:
1)启动Navicat,过滤掉重复数据,采用distict关键字
select DISTINCT(id), xm, zjhm, sfzz, dhhm, xgrq,dw, bm, zw,gw from cs_oth_airport_person
2)点击导出按钮,将数据记录导出成sql文本
3)清空表之后,重新导入数据库
方案二:
1)创建新表
create table cs_oth_airport_person_new like cs_oth_airport_person;
2)插入去重数据
insert into cs_oth_airport_person_new select DISTINCT * from cs_oth_airport_person;
3)删除旧表
drop table cs_oth_airport_person
4) 更改名称
alter table cs_oth_airport_person_new rename to cs_oth_airport_person;
SQL Server
前言
select id,name,age from user a where id in ( select max(id) as id from user b group by age )
这条语句是获取年龄不重复的用户,但是用在SQL Server无法生效
解决方案
从 SQL Server 表中删除重复行 - SQL Server | Microsoft Learn
删除某一个字段相同的记录
删除相同时间的记录,其他的字段都不唯一,也不一定相同,表名video_structure_vehicle,相同数据的字段reportTime,类型datetime
脚本
方案一
先选出不重复的数据,然后导出
select GROUP_CONCAT(distinct reportTime),
`id` ,
`alarmType` ,
`channelId` ,
`address` ,
`description` ,
`num` ,
`image` ,
`alarm_status` ,
`secondImg` ,
`channelIdExit` ,
`numEnter` ,
`numExit` ,
`nameEnter` ,
`nameExit` ,
`imageExit` ,
`alarm_interval` ,
`directionName` ,
`confirmUser` ,
`confirmUserNo` ,
`confirmInfo` ,
`confirmTime` ,
`eventId` ,
`IsReport` ,
`ptz` ,
`repeatCount`
from `video_structure_vehicle` GROUP BY reportTime
方案二
delete from video_structure_vehicle
where reportTime in
(select reportTime from
(select reportTime,count(*) from video_structure_vehicle group by reportTime having count(*) > 1)a)
and id not in
(select id from (select reportTime,min(id) id from video_structure_vehicle where reportTime in
(select reportTime from (select reportTime,count(*) from video_structure_vehicle group by reportTime having count(*) > 1)b)
group by reportTime)c)
注意
mysql要求每一个派生出来的表都必须有一个自己的别名,否则提示报错:
[Err] 1248 - Every derived table must have its own alias