2

I have to delete a row with max(id) from a table in MySQL. I am using query::

 DELETE 
 FROM master 
 WHERE id=(
 SELECT MAX(id) 
 FROM master)

but getting error No. 1093.

Can anybody please help me??

Behnam
  • 6,510
  • 6
  • 35
  • 65
user3796265
  • 17
  • 1
  • 4
  • You cant modify the same table from which you are selecting the data in subquery. – Anik Jul 02 '14 at 05:10
  • possible duplicate of [Mysql error 1093 - Can't specify target table for update in FROM clause](http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause) – brainless coder Jul 02 '14 at 05:10

4 Answers4

6

You can't specify target table for update in FROM clause.

you can delete the last row as mentioned below.

 DELETE FROM master ORDER BY id DESC LIMIT 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
2

You cant modify the same table from which you are selecting the data in subquery.

Try this -

DELETE m.*
FROM master m 
WHERE id IN (SELECT id_temp from(
SELECT MAX(id) as id_temp 
FROM master) x)
Anik
  • 441
  • 4
  • 7
1

You can't specify target table for Delete in FROM clause

Try this

DELETE FROM master 
WHERE id IN (SELECT A.MAXid FROM 
               (SELECT MAX(id) as MAXid FROM master) A
            ) 
anil_g
  • 54
  • 1
  • 8
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0
DELETE FROM Test WHERE id IN(SELECT MAX(id) FROM Test);

This seems to be cross SQL vendor friendly. The LIMIT option in MySQL becomes this in SQL SERVER.

 DELETE FROM Test WHERE id IN(SELECT TOP 1 id FROM Test ORDER BY id DESC );

See here

JGFMK
  • 8,425
  • 4
  • 58
  • 92