SQL加入两个语句

时间:2014-02-18 19:04:34

标签: php sql join

我没有发现任何SQL加入问题这么大,这就是问题:我有两个SQL语句,一个获得资金分配到不同的组中,一个获得了在这些组中花费的钱,如何我会和他们一起加入,以便一起显示“预算”和“费用”吗? 几乎,而不是有部门,资金类别,预算和部门,资金类别,费用表,我想要一个部门,资金类别,预算,费用表。

SELECT st.name as "Department", sft.longname as "Funding Category", sum(sf.amount) as "Budget" 
FROM 
money_type as st, 
money_funding_type as sft, 
money_funding as sf 
WHERE 
st.ID = sf.type_ID AND 
sft.ID = sf.funding_ID 
GROUP BY 
sf.type_id, sf.funding_id 

SELECT st.name as "Department", sft.longname as "Funding Category", sum(si.amount) as "Expenses" 
FROM 
money_type as st, 
money_funding_type as sft, 
money_invoice as si 
WHERE 
st.ID = si.type_ID AND 
sft.ID = si.funding_ID 
GROUP BY 
si.type_id, si.funding_id

3 个答案:

答案 0 :(得分:1)

这样的东西
SELECT  st.name as "Department", 
        COALESCE(sft.longname,sfti.longname) as "Funding Category", 
        sum(sf.amount) as "Budget",
        sum(si.amount) as "Expenses" 
FROM    money_type as st LEFT JOIN

        money_funding as sf  ON st.ID = sf.type_ID LEFT JOIN
        money_funding_type as sft ON sft.ID = sf.funding_ID LEFT JOIN

        money_invoice as si ON st.ID = si.type_ID LEFT JOIN
        money_funding_type as sfti  ON sfti.ID = si.funding_ID
GROUP BY    st.name, 
            COALESCE(sft.longname,sfti.longname)

答案 1 :(得分:0)

使用FULL OUTER JOIN加入发票和资金,然后按资金类型,资金和发票进行分组。

答案 2 :(得分:0)

您可以尝试以下操作。第一个查询“AllTypes”是获取可用数据的不同id /类型,而不是笛卡尔。从那里,简单的连接到类型和资金类型,以获得描述。

然后,另外两个选项简化了他们自己的money_funding / money_invoice元素,只获得他们的“id”字段和金额总和,因此对于每个预算和费用,每种类型/资金将有1个记录。然后将它们连接到“AllTypes”,如果它们具有相应的值,则抓取结果,否则为0.

仅为了可读性而在连接之间有额外的换行符

SELECT
      st.name as "Department", 
      sft.longname as "Funding Category", 
      coalesce( Budgets.Budget, 0 ) as Budget,
      coalesce( Expenses.Expense, 0 ) as Expense,
      coalesce( Budgets.Budget, 0 ) - coalesce( Expenses.Expense, 0 ) as AmtRemain
   from 
      ( select distinct
              type_id,
              funding_id
           from
              money_funding
        union
        SELECT distinct
              type_id,
              funding_id
           from
              money_invoice  ) AllTypes

         JOIN money_type as st
            ON AllTypes.type_id = st.id

         JOIN money_funding_type as sft
            ON AllTypes.funding_id = sft.id

         LEFT JOIN ( SELECT 
                           sf.type_id,
                           sf.funding_id,
                           sum(sf.amount) as "Budget" 
                        FROM 
                           money_funding as sf 
                        GROUP BY 
                           sf.type_id, 
                           sf.funding_id ) Budgets
            ON AllTypes.type_id = Budgets.Type_id
            AND AllTypes.funding_id = Budgets.Funding_ID

         LEFT JOIN ( SELECT 
                           si.type_id, 
                           si.funding_id,
                           sum(si.amount) as "Expense" 
                        FROM 
                           money_invoice as si 
                        GROUP BY 
                           si.type_id, 
                           si.funding_id ) Expenses
            ON AllTypes.type_id = Expenses.Type_id
            AND AllTypes.funding_id = Expenses.Funding_ID

“AllTypes”查询,您可能需要删除第二个“DISTINCT”子句,因为第一个可能适用于整个“AllTypes”查询并且可能会给您一个错误。