【问题标题】:How to get Sum from two tables?如何从两个表中获取总和?
【发布时间】:2012-08-29 20:31:47
【问题描述】:

我有两张表,第一个名字是“sales”,第二个名字是“items” 在两个表中都有相同的列“code”和“qtd”; 我想编写 MYSQL 查询女巫,我需要两个表中的 sum(qtd),其中两个表中的代码相同。 对于单个表,我正在使用这个

"select code,sum(qtd) from sales group by code";

【问题讨论】:

  • 所以你想把两个总和相加吗?
  • 是的,我想要两者的总和
  • cmd.CommandText = "select a.code, sum(a.qtd+b.qtd) from sales a , items b where a.code=b.code group by a.code,b.代码";

标签: c# mysql


【解决方案1】:

试试这个:

Select code, sum(qtd) 
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code

【讨论】:

  • 很抱歉忘记了 all 关键字!我已经编辑了我的答案,请尝试一下。
猜你喜欢
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多