I’m doing a work for a client but since I haven’t been using PHP/MySQL for a while I forgot some simple things, hope you can help me out.
I have the following SQL table:
ID (non-null, autoincrement) | credit (int)
My query should put the whole “credit” column to 0 except for the row that has the higher ID.
So I would do:
UPDATE $table SET credit = 0 WHERE... ?
Thanks in advance for any help 🙂
Will update any rows that have and ID greater than the variable
$IDIf you only want to update the row with the maximum ID then use:
Edit: As Eggyal correctly points out MySQL doesn’t like a subquery on the same table as an update – but you can get around it nicely:
And examples from my console:
Note: As the subquery within a subquery trick unlocks the original table, it is a good idea to run this within a transaction – if the table is unlocked from a query, it might have changed by the time it is updated – so it will be a good idea to use this type of query within a transaction.