我想用另一个表中的当前数量+数量来更新prod_qty;
它在工作台中运行,SET SQL_SAFE_UPDATES = 0;但是当我尝试在VB.net中运行它时发生错误
您无法在FROM子句
中指定目标表'tbl_prod'以进行更新
这是查询
Update tbl_prod
set prod_qty = prod_qty+
(Select itp_needQty
from tbl_itmonpur
where itp_prodID = prod_id
and itp_status = 'PENDING'
and itp_InvoiceNo = 1004)
where prod_id =
(Select itp_prodID
from tbl_itmonpur
where itp_prodID = prod_id
and itp_status = 'PENDING'
and itp_InvoiceNo = 1004);
我只是一名学生,所以请对我很轻松:)谢谢!
con.Open()
qry = "UPDATE tbl_prod " + _
"Join tbl_itmonpur " + _
"ON tbl_itmonpur.itp_prodID = tbl_prod.prod_id " + _
"SET tbl_prod.prod_qty = tbl_prod.prod_qty + tbl_itmonpur.itp_needQty " + _
"WHERE tbl_itmonpur.itp_status = 'PENDING' AND tbl_itmonpur.itp_InvoiceNo @InvoiceNo; "
cmd = New MySqlCommand(qry, con)
With cmd
.Parameters.AddWithValue("@InvoiceNo", txtInvoicePend.Text)
End With
cmd.ExecuteNonQuery()
con.Close()
答案 0 :(得分:0)
看起来所需的数量来自tbl_itmonpur
表,其中itp_prodID
列值与prod_id
表的tbl_prod
列匹配,因此您需要将两个表连接起来,如下所示< / p>
UPDATE tbl_prod
JOIN tbl_itmonpur
ON tbl_itmonpur.itp_prodID = tbl_prod.prod_id
SET tbl_prod.prod_qty = tbl_prod.prod_qty + tbl_itmonpur.itp_needQty
WHERE tbl_itmonpur.itp_status = 'PENDING'
AND tbl_itmonpur.itp_InvoiceNo = 1004;