javascript - SQL 选择和更新语法
问题描述
我正在尝试使用单个语句在 SQL 中执行 SELECT 和 UPDATE。在一些 Google FU 之后,我似乎找不到任何文档。或者,也许我正在寻找错误的东西。
inquirer
.prompt([{
name: "updateID",
type: "input",
message: "What's the ID of the product you'd like to update?"
},
{
name: "updateQuantity",
type: "input",
message: "How many units would you like to add to inventory?"
}]).then(function(answer) {
connection.query("SELECT id, stock_quantity FROM products WHERE ?",
{
id: parseInt(answer.updateID)
}, function(err, res) {
})
})
我希望有一种方法可以在单个语句中执行 SELECT 和 UPDATE。
解决方案
除非您特别想读取该产品的数据,否则您不需要SELECT
在值之前执行 a UPDATE
。根据您的代码,此查询应该执行您想要的操作:
UPDATE products
SET stock_quantity = stock_quantity + ?
WHERE id = ?
如果您还想读取数据,则必须执行单独SELECT
的UPDATE
查询。
推荐阅读
- javascript - 更新位置后 D3 强制链接未与节点连接
- angular - Angular 数据绑定与引用类型
- javascript - 当我尝试从 Firestore 收集时出现问题
- javascript - 需要读取安全cookie并需要检查它是否存在
- angular - binding.node 不是有效的 Win32 应用程序
- google-cloud-platform - 如何使用“gsutil”复制文件夹?
- javascript - 计算 100 分中的 XP 完成百分比
- php - Azure 存储帐户位置变量/常量在哪里
- c# - 第一次后 XAF Mobile DetailView 加载缓慢
- sql - MS-Access SQL - 如何实现布尔子查询?