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'
相关推荐
码农颜15 小时前
5.4.1 锁分类
java·数据库·mysql
云深处@15 小时前
【数据库】MySQL 入门
数据库·学习·mysql
梦远星帆15 小时前
SQLyog社区版下载
数据库·mysql·sqlyog
在世修行21 小时前
从零打造 C# 工业视觉检测系统(九):工厂模式实现 SQL Server / MySQL / SQLite 三库切换
mysql·sqlite·c#·工厂模式
ha_lydms1 天前
Hologres分区表
大数据·mysql·阿里云·实时数仓·hologres·调度·aliyun
JLWcai202510091 天前
核电打磨 “安全卫士”|金利威不锈钢专用树脂砂轮,合规高效双在线
mysql·mongodb·eureka·sqlite·rabbitmq·nosql·memcached
程序员-Benothing1 天前
MySQL 的覆盖索引是什么?
数据库·mysql
星恒讯工业路由器1 天前
MIMO与多天线技术:从5G到WiFi的工程解析
数据库·mysql·5g·工业物联网·mimo技术·天线隔离度·5g/wifi多天线
小Ti客栈1 天前
MySQL查询原理:从Server到InnoDB
android·mysql·adb
2501_937860941 天前
MySQL数据库入门|从零搞懂数据库基础、架构与存储引擎
数据库·mysql·架构