如何根据ID-s对金额进行总计,并将结果添加到现有表的新列中

时间:2014-06-20 10:20:58

标签: mysql sql

我有一个数据库表(下表)快速增长有超过400万行

这是我正在讨论的表格:

#   Name    Type       Collation    Attributes  Null    Default Extra   Action
 1  user_id int(11)         No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 2  amount  decimal(16,8)           No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 3  aff int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 4  jackpot int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 5  paidout int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 6  type    int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 7  created timestamp           No  CURRENT_TIMESTAMP        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More

我通过user_id运行SUM金额的查询,我得到了结果

SELECT user_id, SUM(amount) 
FROM trans
group by user_id;

我的问题是如何将这些结果(SUM-s)添加到另一个现有的表中,我有相同的用户ID并保存这些SUM-s

我现有的表格有这种结构

#   Name    Type    Collation   Attributes  Null    Default Extra   Action
 1  id  int(4)          No  None    AUTO_INCREMENT   Change Change   Drop Drop   Browse distinct values Browse distinct values  Primary Primary  Unique Unique  Show more actions More
 2  balance int(11)         No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 3  ads varchar(60) latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 4  ip  varchar(85) latin1_swedish_ci       No           Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 5  hash    varchar(150)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 6  email   varchar(150)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 7  aff int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 8  with    int(11)         No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More
 9  timestamp   bigint(20)      UNSIGNED    No  0        Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique  Show more actions More

我更愿意将总和放在id列之后的新列上。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

alter table table2 add column thesum decimal(16, 8);

update table2 t2
    set thesum = (select sum(amount) from table1 t1 where t2.user_id = t1.user_id)

现在,这有一些问题。当值在第一个表中更改时,它们不会在第二个表中更改。要实现这一点,您需要使用触发器来重新调整值。

通常,建议对此类流程使用视图。但是,这不是特别有效。我的猜测是,您希望根据性能原因总结这些值。如果是这样,请注意保持值的一致性。

编辑:

要添加总和,只需执行:

update table2 t2
    set thesum = thesum + coalesce((select sum(amount) from table1 t1 where t2.user_id = t1.user_id), 0);