How to delete the last record from a table?

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 not specify target table 'student' for update in FROM clause.

    
asked by anonymous 09.08.2016 / 19:30

3 answers

9

As the 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 have helped!

Any questions, leave a comment.

    
09.08.2016 / 19:40
3

Can not make a DELETE with a subquery by specifying the same table, as it says at documentation .

  

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

    
09.08.2016 / 19:36
0

You can sort by the example table id

DELETE FROM table ORDER BY the field DESC|ASC
    
09.08.2016 / 19:36