I want to delete the most recent entry in MySQL using PHP, is there a function that allows me to do that?
$query2 = "DELETE FROM test WHERE ___";
I want to delete the most recent entry in MySQL using PHP, is there a function that allows me to do that?
$query2 = "DELETE FROM test WHERE ___";
Assuming you have an AUTO_INCREMENT
field on your table, you can do this:
DELETE FROM test WHERE test_id = LAST_INSERT_ID()
You need a datetime column which saved the current time during the INSERT INTO
query. Then you can use the ORDER BY
and LIMIT
keywords to delete the last entry in your table.
DELETE FROM
tab
ORDER BY
datecolumn DESC
LIMIT 1
Check the MySQL 5.1 Reference Manual - 12.2.2 DELETE Syntax for the following statement:
If the DELETE statement includes an ORDER BY clause, rows are deleted in the order specified by the clause. This is useful primarily in conjunction with LIMIT.
DELETE FROM table WHERE id = MAX(id) LIMIT 1
should also be working if you are using auto increment