如何从两个表中获取总和? [英] How to get Sum from two tables?
本文介绍了如何从两个表中获取总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表第一个名字是销售和第二个名字是项目
两个表中有相同的列代码和QTD;
我想编写MySQL查询巫婆,我需要从两个表的总和(QTD),其中的代码是在两个表相同。
单表我使用这个
选择代码,代码从销售组和(QTD);
< DIV CLASS =h2_lin>解决方案
试试这个:
选择代码,SUM(QTD)从
(
选择代码,从销售
工会QTD所有
选择代码,从项目QTD)作为innerTable
组由码
i have two tables first one name is "sales" and second one name is "items" in both tables have same columns "code" and " qtd "; i want write MYSQL query witch i need sum(qtd) from both of tables where code is same in both of tables . for single table i am using this
"select code,sum(qtd) from sales group by code";
解决方案
Try this:
Select code, sum(qtd)
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code
这篇关于如何从两个表中获取总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文