tengo una tabla que se actualiza aleatoriamente más de una vez al día. Cada actualización contiene alrededor de 2000 filas. Quiero mantener el último conjunto de datos por día y eliminar las filas más antiguas. Construí un ejemplo:
La tabla "fecha" contiene:
+-------+-------+---------+---------------------+ | title | hhm | hhm_sum | updated | +-------+-------+---------+---------------------+ | 74142 | 21525 | 5874136 | 2020-06-15 00:00:00 | | 74142 | 5263 | 2145 | 2020-06-22 00:00:00 | | 74142 | 21254 | 21458 | 2020-06-22 04:00:00 | | 74142 | 21458 | 3652 | 2020-06-22 08:00:00 | | 74142 | 2158 | 1257 | 2020-06-20 00:00:00 | +-------+-------+---------+---------------------+ Con: SELECT * FROM test.date WHERE updated > DATE_SUB(DATE(NOW()), INTERVAL 24 HOUR);
Obtengo los conjuntos de datos de las últimas 24 horas:
+-------+-------+---------+---------------------+ | title | hhm | hhm_sum | updated | +-------+-------+---------+---------------------+ | 74142 | 5263 | 2145 | 2020-06-22 00:00:00 | | 74142 | 21254 | 21458 | 2020-06-22 04:00:00 | | 74142 | 21458 | 3652 | 2020-06-22 08:00:00 | +-------+-------+---------+---------------------+Quiero eliminar las filas más antiguas el mismo día y mantener las más nuevas. ¿Cómo puedo obtener y manejar este valor?
¿Quizás alguien pueda ayudar?
Muchísimas gracias.
Si desea mantener la única fila más nueva, ordene por updated desc y tome la primera fila.
select * from test_date order by updated desc limit 1 Si puede haber varias filas que tengan la updated más reciente, haga una subconsulta para obtener el max(updated) .
select * from test_date where updated = ( select max(updated) from test_date )