I am trying to run this:
$stmt=$cxn->prepare("UPDATE table1 SET used='1' WHERE prim_id !=
(SELECT MAX(prim_id) FROM table1 WHERE email='email12345@gmail.com')");
$stmt->execute(array());
But it results in a MySQL error: #1093 - You can't specify target table 'table1' for update in FROM clause.
After searching this error, it seems that In MySQL, you can't modify the same table which you use in the SELECT part..
How can I change that query to make it work?
Thanks a lot in advance
Try this:
It removes the subquery and uses a regular temporary table to store the result of table “tmp”.
The query takes 2 tables for update:
The WHERE clause:
The SET clause says that only t1.used will be updated.
UPDATE statement