Rick Joe Rick Joe - 2 months ago 8
MySQL Question

mysql select and update using one query in this case?

Can I select and update together? Instead of use 2 queries, can I use just one?

SELECT data FROM log limit 1

update log SET `data` = $now where `id` = 1


any ideas?

Answer

Lock the table to prevent another user from updating the table.

LOCK TABLES data;
SELECT data FROM log WHERE id = 1;
// fetch the result
UPDATE log SET data = NOW() WHERE id = 1;
UNLOCK TABLES;
Comments