簡體   English   中英

從一個表的列到另一表的列的mysql數據減法

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM