I'm trying to do a SELECT and a UPDATE in SQL, using a single Statement. After some Google FU i can't seem to find any documentation on it. Or maybe i'm looking for the wrong thing.
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) {
})
})
I'd hope there's a way to do a SELECT and UPDATE in a single statement.
Unless you specifically want to read the data for that product, you don't need to do a SELECT
before you UPDATE
the value. Based on your code, this query should do what you want:
UPDATE products
SET stock_quantity = stock_quantity + ?
WHERE id = ?
If you do want to read the data as well, you have to perform separate SELECT
and UPDATE
queries.
I want to SELECT a row in the table by the ID, then UPDATE one the values in a column of that row...
In order to update only certain rows, you can use a single UPDATE
query with a WHERE
clause.
UPDATE <table> SET <column>=<value>
WHERE <id_column>=<id>;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.