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'
相关推荐
2401_8368365939 分钟前
mysql集群
mysql·adb
ZHOU_WUYI1 小时前
MySQL 与 FastAPI 交互教程
mysql·fastapi
Code哈哈笑1 小时前
【基于Spring Boot 的图书购买系统】深度讲解 用户注册的前后端交互,Mapper操作MySQL数据库进行用户持久化
数据库·spring boot·后端·mysql·mybatis·交互
Nuraliye1 小时前
IP到国家代码映射之GeoLite2导入到MySQL形成数据字典
mysql·geolite2·geoip_country_blocks·geoip_country_locations·ip到国家代码映射
caihuayuan43 小时前
鸿蒙AI开发:10-多模态大模型与原子化服务的集成
java·大数据·sql·spring·课程设计
lyrhhhhhhhh3 小时前
MyBatis 延迟加载与缓存
sql·缓存·mybatis
Musennn3 小时前
MySQL多条件查询深度解析
大数据·数据库·mysql
伤不起bb7 小时前
MySQL 高可用
linux·运维·数据库·mysql·安全·高可用
运维成长记13 小时前
mysql数据库-中间件MyCat
数据库·mysql·中间件
尘客.13 小时前
DataX从Mysql导数据到Hive分区表案例
数据库·hive·mysql