[英]MySQL - delete entry created within seconds/range
I am using MySQL database with following structure:我正在使用具有以下结构的 MySQL 数据库:
sensor value timestamp 1 10 2017-01-25 06:00:00 2 12 2017-01-25 06:20:00 2 12 2017-01-25 06:20:05 3 30 2017-01-25 06:23:00
As you can see for sensor 2 there are multiple entries, which were created within a few seconds.如您所见,传感器 2 有多个条目,这些条目是在几秒钟内创建的。 I would like to delete one of those 2 entries, which have been created in a time range of 10 seconds.
我想删除这 2 个条目之一,这些条目是在 10 秒的时间范围内创建的。 How can I select/delete them?
如何选择/删除它们?
Can someone help me with this?有人可以帮我弄这个吗?
You can use a IN clause on the max timestamp tuple您可以在最大时间戳元组上使用 IN 子句
delete from my_table
where (sensor, timestamp) in (
select sensor, max(timestamp)
from my_table
group by sensor,
having count(*) >1
)
and for time range for duplicated deletion of 10 seconds以及重复删除的时间范围为 10 秒
delete from my_table
where (sensor, timestamp) in (
select sensor, max(timestamp)
from my_table
group by sensor, UNIX_TIMESTAMP(timestamp) DIV 10
)
and for avoid the select/delete table you could use a join on a subselect为了避免选择/删除表,您可以在子选择上使用连接
delete from my_table
inner join (
select sensor, max(timestamp) my_max
from my_table
group by sensor, UNIX_TIMESTAMP(timestamp) DIV 10
) t on t.sensor = my_table.sensor and t.my_max = my_table.timestamp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.