![](/img/trans.png)
[英]MySQL - Taking data from one column in a table and inserting it into another table
[英]mysql data substraction from column of one table to column of another table
我有兩個名為1. users 2. transaction
不同表1. users 2. transaction
且兩個表都有“金額”名稱列。
我需要從transaction-> amount
列中減去users-> amount
列,然后將減去的值存儲到users-> amount
列中。
表結構:
users have column: id(primary key),username,amount
transaction have column: id (primary key),username,transaction_id,amount
在這里我想要users.amount = users.amount - transaction.amount
其中兩個表的id
都相同。
請嘗試以下更新查詢:
UPDATE users t1
INNER JOIN transaction t2
ON t1.id = t2.id
SET t1.amount = t1.amount - t2.amount
如您所說,這假設兩個表的id
列都標識一個用戶。 它還假定您要無限制地更新users
表中的每條記錄。 如果沒有,則可以輕松地將WHERE
子句添加到更新查詢中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.