简体   繁体   English

如何将值添加到列字段中的现有值,然后将更新值插入另一个表

[英]How to add a value to already existing value in column field and then insert the update value into another table

I Have a members table with a column amount which already has an integer value in it, I wish to add to that value and then use its contents to update another table called accounts which holds the amount being credited and the new balance (ie After addition) 我有一个members表,其中的列amount已经有一个整数值,我希望添加到该值,然后使用其内容更新另一个称为accounts表,该表包含贷记金额和新余额(即,加)

   $this->query_array = array(':amt' =>  $amount, ':uid' => $userid, ':dat' => $date );

    $this->query_string = "UPDATE members SET amount = amount + :amt;
                           INSERT INTO accounts VALUES ( :uid, :amt, members.amount + :amt,  :dat);";

Thats my query above, am trying to get the members.amount table from the update query and use it in the insert query is there a better way, one that works than what I tried. 那就是我上面的查询,我试图从update查询中获取members.amount表并在insert查询中使用它,这是一种更好的方法,一种比我尝试过的方法更有效的方法。

Sample Data : 样本数据 :

         Members Table      userid       amount
                             ---------------------
                               u123        4000
                               y123        5000

          Accounts Table      userid       credit   balance    date

Accounts table is currently empty so what I wanna do is add 300 to all values in memebrs table and then insert into accounts something like this. Accounts table当前为空,所以我想做的是将300加到memebrs表中的所有值,然后将类似的内容插入帐户。

          Accounts Table      userid       credit   balance    date
                              -------------------------------------
                                u123       300      4300       11-11-11
                                y123       300      5300       11-11-11

It's a simple insert by a select query, the only question is about the date 这是选择查询的简单插入,唯一的问题是关于日期

INSERT INTO Accounts (userid, credit, balance, date)
  SELECT userid, 300, amount, NOW()
    FROM Members;

I would use an insert ... select ... statement to achieve the desired outputs: 我将使用insert ... select ...语句来实现所需的输出:

INSERT INTO accounts
SELECT members.userid, :amt, members.amount,  :dat FROM members

Notes: 笔记:

  1. I'm using the members.amount in place of members.amount + :amt because the :amt has already been added once to members.amount . 我使用members.amount代替members.amount + :amt因为:amt已经被添加到members.amount一次。

  2. Your update statement updates all records within the members table, yet the insert statement would create records for a specific user only. 您的更新语句将更新members表中的所有记录,但是insert语句将仅为特定用户创建记录。 I believe you should restrict the update statement to the specific user only or not restrict the insert to a specific user. 我相信您应该只将update语句限制为特定用户,或者不将插入内容限制为特定用户。 I chose the latter approach. 我选择了后一种方法。

  3. If you want to store the balance in a denormalised way, then I would rather use a trigger on the accounts table to update the balance in the members table, than to use 2 statements. 如果您想以非规范化的方式存储余额,那么我宁愿使用accounts表上的触发器来更新members表中的余额,而不是使用2条语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM