MySQL UPDATE and SELECT in one pass

后端 未结 6 1380
轮回少年
轮回少年 2020-11-28 14:05

I have a MySQL table of tasks to perform, each row having parameters for a single task.
There are many worker apps (possibly on different machines), performing tasks in

6条回答
  •  生来不讨喜
    2020-11-28 14:47

    I have the exact same issue. We ended up using PostreSQL instead, and UPDATE ... RETURNING:

    The optional RETURNING clause causes UPDATE to compute and return value(s) based on each row actually updated. Any expression using the table's columns, and/or columns of other tables mentioned in FROM, can be computed. The new (post-update) values of the table's columns are used. The syntax of the RETURNING list is identical to that of the output list of SELECT.

    Example: UPDATE 'my_table' SET 'status' = 1 WHERE 'status' = 0 LIMIT 1 RETURNING *;

    Or, in your case: UPDATE 'tasks' SET 'guid' = %d WHERE 'guid' = 0 LIMIT 1 RETURNING 'params';

    Sorry, I know this doesn't answer the question with MySQL, and it might not be easy to just switch to PostgreSQL, but it's the best way we've found to do it. Even 6 years later, MySQL still doesn't support UPDATE ... RETURNING. It might be added at some point in the future, but for now MariaDB only has it for DELETE statements.

    Edit: There is a task (low priority) to add UPDATE ... RETURNING support to MariaDB.

提交回复
热议问题