Skip to main content
2 de 2
O OP solicitou o comando de DELETE e não o comando de SELECT.
Leandro Amorim
  • 1,9mil
  • 10
  • 19

No MySQL 8.0 existe a possibilidade de usar RANK()

SELECT
    id, usuario_id, status
FROM (
  SELECT 
    id, usuario_id, status,
    RANK() OVER (PARTITION BY usuario_id ORDER BY id DESC) AS intRow
  FROM
    Table1
  WHERE
    status = 0
) T
WHERE
    intRow = 1

https://www.db-fiddle.com/f/7V6HBDsCmd4m9uPiz9cnWY/0

Para deletar os registros com ID repetido você poderia usar um código como esse:

DELETE FROM Table1
WHERE
    id IN (
      SELECT
          id
      FROM (
            SELECT id, RANK() OVER (PARTITION BY usuario_id ORDER BY id DESC)  AS intRow 
            FROM Table1 WHERE status = 0
      ) T WHERE intRow > 1
    );

https://www.db-fiddle.com/f/7V6HBDsCmd4m9uPiz9cnWY/2

Leandro Amorim
  • 1,9mil
  • 10
  • 19