如何组合2个不同的表? [英] how to combine 2 different table?

查看:131
本文介绍了如何组合2个不同的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何组合包含相同字段但不同数据的2个不同表格,例如
Cash_Expenses

how to combine 2 different tables where they contain the same fields but different data, example Cash_Expenses

{
exp_date
exp_cat_id
exp_amount
exp_remark
}


$ b b

Cheque_Espenses

Cheque_Espenses

{
exp_date
exp_cat_id
exp_cheque_NO
exp_amount
exp_remark
}

exp_cat

{
cat_id
Cat_name
}

现在我想做的是,我想要结合这三个和总金额到其各自的猫,当我使用这个sql statment

now what i am trying to do is that i want to combine those three and sum the amount to its respective cat, where when i use this sql statment

SELECT DISTINCT exp_cat.cat_name, Sum(exp_cash.exp_amount) AS SumOfexp_amount, Sum(exp_cheque.exp_amount) AS SumOfexp_amount1
FROM (exp_cat INNER JOIN exp_cheque ON exp_cat.ID = exp_cheque.exp_cat_id) LEFT JOIN exp_cash ON exp_cat.ID = exp_cash.exp_cat_id
GROUP BY exp_cat.cat_name;

我得到重复的总和不正确,任何建议我很高兴为任何人学习


i get duplications where the sum is incorrect, any suggestion i well be glad to learn for anyone

推荐答案

这应该让你接近:

select cat_name, sum(exp.exp_amount)
from (select exp_cat_id, exp_amount from cash_expenses
      union all
      select exp_cat_id, exp_amount from cheque_expenses) as exp
inner join exp_cat on exp.cat_id = exp_cat.cat_id
group by cat_name;

这篇关于如何组合2个不同的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆