dongqiancui9194 2013-11-08 23:15
浏览 30

MySQL DB更新2值:1增加1减少

I have a table in my database called member within this table are the fields 'personID&balance`.

If a user 1 has a balance of 20.00 and wishes to send 10.00 to user 2 what is the best way to reduce user 1's balance and update user 2's balance?

Any help/advice would be great!

Thanks in advance

UPDATE

CODE:

 $accountfrom=$_POST[accountfrom];
 $accountto=$_POST[accountto];
 $amount=$_POST[amount]; 
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con) 
  {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("cl49-XXX", $con)or die( "Unable to select database");


$result = mysql_query("UPDATE member
SET balance = IF(personID = $acountfrom, balance-$amount, balance+$amount)
WHERE personID IN ($accountfrom, $accountto)")
 or die(mysql_error());

RESULT:

'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' balance-100.00, balance+100.00) WHERE personID IN (84587745 , 98554130)' at line 2'

  • 写回答

1条回答 默认 最新

  • douao7937 2013-11-08 23:18
    关注
    UPDATE member
    SET balance = IF(personID = 1, balance-10, balance+10)
    WHERE personID IN (1, 2)
    

    To limit the transfer to the sender's balance:

    UPDATE member m1
    CROSS JOIN (SELECT LEAST(10, balance) transfer
          FROM member
          WHERE personID = 1) m2
    SET m1.balance = IF(personID = 1, balance - transfer, balance + transfer)
    WHERE personID in (1, 2)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用