How to delete the last record from a table?

Asked

Viewed 1,297 times

7

I was using the following code in mysql:

delete from aluno where id=(select MAX(id) from aluno);

But the following error appears:

Error Code: 1093. You can’t specify target table 'student' for update in FROM clause.

3 answers

9


As Rray said, it is not possible, but there are alternatives... An alternative would be to use variables:

set @val_max = (select max(id) from aluno  );
delete  FROM aluno  where id = @val_max;

I hope I’ve helped!

Any doubt, leave a comment.

3

Unable to perform a DELETE with a subquery specifying the same table, as it says documentation.

You cannot delete from a table and select from the same table in a subquery.

0

You can sort by the example table id

DELETE FROM table ORDER BY the field DESC|ASC
  • 1

    The clause ORDER BY does not apply to DELETE. If this worked, it would simply delete all records from the table.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.