一键执行MySQL UPDATE和SELECT
我有一个要执行的MySQL任务表,每一行都有一个任务的参数.
有许多工作人员应用程序(可能在不同的计算机上),可以循环执行任务.
这些应用程序使用MySQL的本地C API访问数据库.
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 a loop.
The apps access the database using MySQL's native C APIs.
为了拥有任务,应用程序需要执行以下操作:
In order to own a task, an app does something like that:
-
生成一个全局唯一的ID(为简单起见,它是一个数字)
Generate a globally-unique id (for simplicity, let's say it is a number)
UPDATE tasks
SET guid = %d
WHERE guid = 0 LIMIT 1
UPDATE tasks
SET guid = %d
WHERE guid = 0 LIMIT 1
SELECT params
FROM tasks
WHERE guid = %d
SELECT params
FROM tasks
WHERE guid = %d
如果最后一个查询返回一行,则表明我们拥有该行并具有要运行的参数
If the last query returns a row, we own it and have the parameters to run
是否可以通过一次调用服务器来实现相同的效果(即拥有"一行并获取其参数)?
Is there a way to achieve the same effect (i.e. 'own' a row and get its parameters) in a single call to the server?
尝试这样
UPDATE `lastid` SET `idnum` = (SELECT `id` FROM `history` ORDER BY `id` DESC LIMIT 1);
上面的代码对我有用