mysql - 从第二个表中的值减去一个表中的值 - 将结果设置为第二个表中的值

标签 mysql join sql-update

我有两张 table 。第一个表是 Shipping,其中包含 ProductQuantityID 列。第二个表是 Inventory,其中包含 ProductTotal 列。

我想获取 Shipping.Quantity 的值,并从 Inventory.Total 中的值减去它,其中 Product 值与每个表和 Shipping.ID 都是 url ($onum) 中的值。然后我想将该产品的新值设置为 Inventory.Total。

我能想到的最好的:

UPDATE Inventory.Total 
CROSS JOIN Shipping 
SET Inventory.total=(Inventorytotal-Shipping.Quantity) 
WHERE Inventory.Product=Shipping.Product 
  AND Shipping.ID=$onum

最佳答案

UPDATE Inventory join Shipping 
ON Inventory.Product=Shipping.Product 
SET Inventory.Total=Inventory.Total-Shipping.Quantity
WHERE Shipping.ID=$ID

关于mysql - 从第二个表中的值减去一个表中的值 - 将结果设置为第二个表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13235911/

相关文章:

mysql - Laravel 4 的列值不正确

php - 显示按月分组的 mysql 表中的数据

mysql - 从多个表中获取带时间戳的计数

sql - 每个工作日按每小时分组

python - 连接多个数据帧时保留原始分类映射

MySql 异常未处理 - 命令执行期间遇到 fatal error

java - Java中的多个爬虫数据库连接

php - 在 INSERT (PHP) 中将 2 个不同的查询合并为一个

MySQL 更新其他表中的多行多个值,给出重复值

sql - 从同一表的一列更新多列值