MySQL UPDATE and SELECT in one pass(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)
更新任务SET guid = %dWHERE guid = 0 LIMIT 1
选择参数来自任务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);
以上代码对我有用
这篇关于MySQL UPDATE 和 SELECT 一次完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL UPDATE 和 SELECT 一次完成
基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
