【问题标题】:Insert multiple values and sum php+mysql插入多个值并求和 php+mysql
【发布时间】:2014-01-28 05:37:58
【问题描述】:

我希望有人可以帮助我。

我想将表 A 中的所有数据复制到表 B 中,并对表 A 的一些值求和,然后将结果与其余信息一起插入表 B。

这是我的句子:

INSERT INTO carga ( Item, REC_Rep, Invoices, Description, Qty, Skids, Boxes, Bins, Metal_Rack, Totes, Other, tqty, tskids, tboxes, tbins, tmetal, ttotes, tother) 
SELECT DISTINCT Item, REC_Rep, Invoices, Description, Qty, Skids, Boxes, Bins, Metal_Rack, Totes, Other, SUM(Qty) AS Qty, SUM(Skids) AS Skids, SUM(Boxes) AS Boxes, SUM(Bins) AS Bins, SUM(Metal_Rack) AS Metal, SUM(Totes) AS Totes, SUM(Other) AS Other 
FROM tmp_carga

如果我运行这些句子,我只得到一个寄存器。

如何从 Tabla A (tmp_carga) 获取所有数据或寄存器,对一些值求和并将这些值与其余信息一起插入到 Tabla B(carga) 中?

非常感谢您的帮助!

【问题讨论】:

  • 你应该考虑使用GROUP BY
  • 感谢您提供的信息。

标签: mysql


【解决方案1】:

您想使用聚合函数 SUM,但您没有对选择查询中的任何内容进行分组。这创造了一切的“总和”。在您的选择(即项目)和 group by 上找到唯一键

【讨论】:

  • 谢谢你的信息,现在我得到了所有的记录,但不幸的是我没有得到值的总和。你能告诉我这句话是否正确:SELECT DISTINCT Item, REC_Rep, Invoices, Description, Qty, Skids, Boxes, Bins, Metal_Rack, Totes, Other, SUM(Qty) AS Qty, SUM(Skids) AS Skids, SUM( Boxes) A​​S Boxes, SUM(Bins) AS Bins, SUM(Metal_Rack) AS Metal, SUM(Totes) A​​S Totes, SUM(Other) AS Other FROM tmp_carga 非常感谢您的宝贵时间
猜你喜欢
  • 2019-03-24
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
相关资源
最近更新 更多