I’ve got a query with two tables. The stockmaster table comes in second after the prodstock table. Now that I want to update both tables by running two queries, I want to run a query using a function, but I can’t, as I will explain in more detail below.
This is stockmaster table
This is prodstock table
here is the code of the function
export const postStock = (body) => {
let sql = ` INSERT INTO stockmaster (stocknum, cat_id, user_id, dyenumber, stockQty, price,stockform, remark) VALUES ('${body.stocknum}', '${body.cat_id}', '${body.user_id}', '${body.dyenumber}', '${body.stockQty}', '${body.price}', '${body.stockform}', '${body.remark}')`;
return sql;
};
export const updateprodStock = (cat_id, dyenumber, stockQty) => {
let sql = `UPDATE prodstock JOIN stockmaster ON prodstock.cat_id = '${cat_id}' AND prodstock.dyenumber = '${dyenumber}' SET prodstock.total_qty = prodstock.total_qty + '${stockQty} `
return sql}
and here where both function are called
static stock = (req, res) => {
const { cat_id, dyenumber, stockQty } = req.body;
connection.query(postStock(req.body), (err, result) => {
if (err) {
throw new Error(err);
} else {
connection.query(updateprodStock(cat_id, dyenumber, stockQty))
res.status(200).json({
code: 1,
msg: "success",
data: result
})
}
})
}
So, in order for the stock to be updated and the total quantity to be updated in one function, I also want to run the updateprodStock command when I perform the poststock function. However, this is not working. Can anyone offer assistance?
2
Answers
Okay Here I solved this problem by myself I have to call just another query under the first query using function now it looks like following and successfully working it is as follows:-
I just called another query into the old function and it solved everything .....
In my opinion, this line is not perfect:
It will be ( after adding variables):
You can alco consider of using bind variables insteadof literals. Anyway, this update should look like that: