Skip to main content
O OP solicitou o comando de DELETE e não o comando de SELECT.
Fonte Link
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

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

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

Fonte Link
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