2015-04-29 57 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'; 
相关问题