How to delete old records according to the date

0

I am working with tables in mysql and I am not able to delete the oldest records with respect to the dates, there are several users who have several records but I just want to leave the most recent records of each user

select min(updated_at) as olds
from vital_signs  group by id order by patient_id

This would be my query in SQL that brings me all the records I want to delete, but when I add the delete I get an error

    
asked by JCVN08 18.10.2018 в 14:40
source

1 answer

0

Good morning everyone.

In the end I could solve my problem the solution was on the other hand but I share, first I made a select to get all the last records of each user and then use another select to get the id of that select , later use the corresponding delete to erase what you did not need

delete from vital_signs where id not in(select x.ide from (select max(id) as ide,patient_id,max(updated_at) as olds from vital_signs group by patient_id order by patient_id) as x)

    
answered by 18.10.2018 в 22:24