2015-04-29 87 views
0

我需要模擬從一個帳戶到另一個帳戶的匯款。該表是這樣的:Mysql - 匯款:將數據從一個字段傳輸到另一個字段到同一個表

CREATE TABLE new_table 
(ID int NOT NULL AUTO_INCREMENT, 
Name varchar(50) NOT NULL, 
Password varchar(50) NOT NULL, 
Dollars double, 
PRIMARY KEY (ID) 
); 

我能不能在MySQL中找出一個語句將數據從一個細胞/場(美元)轉移到另一個。它必須將轉移金額添加到單元格中,並從轉移單元格中提取相同的金額。

回答

0

SQL語句將更新存款和取款的美元列列。要看到它的行動,看看這個SQL Fiddle demo。在下面的例子中,$ 150從Bob的賬戶轉移到Sara's。

Set @TransferAmount = 150; 
update new_table withdraw, new_table deposit 
set withdraw.Dollars = withdraw.Dollars - @TransferAmount, 
    deposit.Dollars = deposit.Dollars + @TransferAmount 
Where withdraw.name = 'Bob Smith' and deposit.name = 'Sara Hampton'; 
+0

嗨@tobias_jac如果這個或任何答案已經解決你的問題,請考慮[接受它](http://meta.stackexchange.com/q/5234/179419)通過點擊複選標記。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – WorkSmarter

0

你不想在一個事務中簡單地選擇和更新這些操作。當你沒有提供有關什麼框架/庫您使用的任何信息,我不能告訴你究竟怎麼了......

But what you need is 
    SET @payers_money= select dollars from new_table where Name = 'Name of paying person'; 
    SET @receivers_money=select dollars from new_table where Name = 'Name of receiving person'; 
    SET @payment=10; -- for example 
SET @[email protected][email protected]; 
SET @[email protected][email protected]; 
    update new_table SET dollars [email protected]_money where name= 'Name of paying person'; 
    update new_table SET dollars = @receivers_money where name= 'Name of person being paid'; 
相關問題