Page 1 of 1

deleting the last inserted row in DB

Posted: Sat Apr 02, 2005 1:52 am
by thallish
hey all

I have a dream that it one day would be possible to use the following code snippet

Code: Select all

// the newly inserted row in table 'education' is deleted
$query = &quote;DELETE FROM table WHERE id=MAX(id)&quote;;
but it's not working. Probably because of the wrong use of the MAX-function(at least that's what mysql_error() tells me)

So is it really necessary to count the rows, set $id=count_result and then delete where id='$id', or is there another smarter way of doing it?

thallish

Posted: Sat Apr 02, 2005 5:12 am
by n00b Saibot
if your id is auto-increment then you can get last id generated by

Code: Select all

SELECT LAST_INSERT_ID()

Posted: Sat Apr 02, 2005 7:39 am
by feyd
FYI, number of records and the ID have little to do with each other. Reason: delete a record. You'll have a discrepancy.

Posted: Sat Apr 02, 2005 7:58 am
by thallish
yeah know that :wink:

just explained it akwardly. It was meant to be the last inserted row == the one with the highest id.

I might as well ask on a suggestion here. I have some tables I want to put some data into. If one insert gets wrong i'll have to correct my previous inserts. Is there a best way to do that? My db doesn't support INSERT ALL

thallish

Posted: Sat Apr 02, 2005 8:48 am
by feyd