How would I delete yesterday's duplicate data from a MySQL table and keep latest record?
For example, with the following data:
SELECT * FROM data; | pid | serviceid | id | created | ----| ----------|----------------|--------- | 10 | ABNCKN | Q0CHZ2GI1VKOPL | 2022-06-14 10:49:34 | 11 | ABNCKN | Q0CHZ2GI1VKOPL | 2022-06-14 10:49:34 | 12 | KSPSLS | QLSPDF0S0SDFKK | 2022-06-15 11:44:21 | 13 | AKNKSL | QLSPDF0S0SDFKK | 2022-06-15 12:51:42 | 14 | AKNKSL | QLSPDF0S0SDFKK | 2022-06-15 12:51:42 I used
DELETE n1 FROM data n1, data n2 WHERE n1.pid < n2.pid AND n1.id = n2.id How could I DELETE to only remove duplicates and keep latest pid for yesterday's data?
Expected output would be
SELECT * FROM data; | pid | serviceid | id | created | ----| ----------|----------------|--------- | 10 | ABNCKN | Q0CHZ2GI1VKOPL | 2022-06-14 10:49:34 | 11 | ABNCKN | Q0CHZ2GI1VKOPL | 2022-06-14 10:49:34 | 12 | KSPSLS | QLSPDF0S0SDFKK | 2022-06-15 11:44:21 | 14 | AKNKSL | QLSPDF0S0SDFKK | 2022-06-15 12:51:42 remove | 13 | AKNKSL | QLSPDF0S0SDFKK | 2022-06-15 12:51:42