mysql 删除重复数据 关联自己 关联子查询 delete

有手工录入的数据时,删除系统定时任务计算的数据。

sql 复制代码
delete from t1 using data_tab as t1, 
	(select * from (
	       select input_type,system_code,DATE_FORMAT(start_time,'%Y-%m-%d') as date_  from data_tab where 
	       start_time >='2024-05-20' and start_time <'2024-05-27'
	       group by system_code,DATE_FORMAT(start_time,'%Y-%m-%d'),input_type
	       )t 
       group by system_code,date_ HAVING count(1)>1) as t2 
where t1.system_code = t2.system_code and DATE_FORMAT(t1.start_time,'%Y-%m-%d') = t2.date_ 
and t1.start_time >='2024-05-20' and t1.start_time <'2024-05-27'
and t1.input_type='sys'

sql分析,delete from 后面跟着的,是需要删除数据的表。关联的表是查询出来的结果表。

注意:牢记!!!一定要给删除做条件限制,例如:时间,状态,类别,等等。这些常规的,在做删除操作的时候,要作为惯性。不然会删除大量数据。

当然,在删除之前,一定要用select语句检验一下,看看条件是否正确。

sql 复制代码
select * from  data_tab t1
left join (select * from (
	       select input_type,system_code,DATE_FORMAT(start_time,'%Y-%m-%d') as date_  from data_tab where 
	       start_time >='2024-05-01' and start_time <'2024-05-08'
	       group by system_code,DATE_FORMAT(start_time,'%Y-%m-%d'),input_type
	       )t 
       group by system_code,date_ HAVING count(1)>1) as t2 
       on t1.system_code = t2.system_code and DATE_FORMAT(t1.start_time,'%Y-%m-%d') = t2.date_
where 
 t1.start_time >='2024-05-01' and t1.start_time <'2024-05-08'
and t1.input_type='sys'
and t1.system_code = '1339849364182729817'
相关推荐
weelinking3 小时前
【产品】00_产品经理用Claude实现产品系列介绍
数据库·人工智能·sql·数据挖掘·github·产品经理
小江的记录本5 小时前
【Java基础】泛型:泛型擦除、通配符、上下界限定(附《思维导图》+《面试高频考点清单》)
java·数据结构·后端·mysql·spring·面试·职场和发展
bqq198610267 小时前
MySQL 8与MySQL 5.7的主要区别
数据库·mysql
chushiyunen8 小时前
r树索引、mysql对r树的支持
数据库·mysql
罗超驿8 小时前
16.深入理解数据库事务:从转账场景剖析ACID四大特性与回滚(Rollback)机制
数据库·mysql
杨云龙UP9 小时前
Oracle RAC/ODA环境下如何准确查询PDB表空间已分配大小?一次说清Oracle表空间逻辑大小和ASM三副本实际占用_2026-05-19
linux·运维·数据库·sql·oracle·ffmpeg
@nengdoudou9 小时前
KingbaseES数据库MySQL模式使用 “GROUP BY“
数据库·mysql
Wait....11 小时前
死锁的知识总结
数据库·mysql
逻辑羊驼11 小时前
VSCODE 连接 MySQL 数据库并执行当地SQL文件
数据库·mysql
夜白宋11 小时前
【Mysql深入】二、事务
数据库·mysql