我想合并两个查询有一个结果

时间:2017-11-16 06:26:33

标签: mysql sql join

这是我的第一个查询,它给出了与工资相关的实际和估计费用总和

SELECT
  d.y_                                   AS `salaries`
 , IFNULL(SUM(d.actual),0)               AS `Estimated`
 , IFNULL(SUM(d.estimated),0)            AS `Actual`
FROM (
SELECT estimated_type                       AS`y_`
    ,null                                       AS `Estimated`
    , sum(estimated_amount)                     AS `Actual`
FROM bacci.estimated_expenses_table it
WHERE it.estimated_year = '2017'
    AND it.estimated_type = 'management'
GROUP
    BY YEAR(it.estimated_year)
         union all
SELECT Type_expenses_table                      AS`y_`
    ,SUM(it.Amount_expenses_table)              AS `Estimated`
    , null                                      AS `Actual`
FROM bacci.expenses_table it
WHERE it.Date_expenses_table >= '2017-01-01'
        AND it.Date_expenses_table  < '2017-01-01' + INTERVAL 1 YEAR
        AND it.Type_expenses_table = 'management'
GROUP
     BY YEAR(it.Date_expenses_table)
         union all
SELECT estimated_type                           AS`y_`
    ,null                                       AS `Estimated`
    , sum(estimated_amount)                     AS `Actual`
FROM bacci.estimated_expenses_table it
WHERE it.estimated_year = '2017'
    AND it.estimated_type = 'staff salaries'
GROUP
     BY YEAR(it.estimated_year)
         union all
SELECT Type_expenses_table                      AS`y_`
    ,SUM(it.Amount_expenses_table)              AS `Estimated`
    , null                                      AS `Actual`
FROM bacci.expenses_table it
WHERE it.Date_expenses_table >= '2017-01-01'
        AND it.Date_expenses_table  < '2017-01-01' + INTERVAL 1 YEAR
        AND it.Type_expenses_table = 'staff salaries'
GROUP
     BY YEAR(it.Date_expenses_table)
) d
 GROUP
 BY d.y_

ORDER
 BY d.y_;

结果:

enter image description here

我的第二个查询是它给出了与设备相关的实际和估计费用的总和

SELECT
  d.y_                                   AS `Equipments`
 , IFNULL(SUM(d.actual),0)               AS `Estimated`
 , IFNULL(SUM(d.estimated),0)            AS `Actual`
FROM (
    select estimated_type                       AS`y_`
    ,null                                       AS `Estimated`
    , sum(estimated_amount)                     AS `Actual`
FROM bacci.estimated_expenses_table it
WHERE it.estimated_year = '2017'
    and it.estimated_type = 'vehicle rent'
GROUP
     BY YEAR(it.estimated_year)
         union all
SELECT Type_expenses_table                      AS`y_`
    ,SUM(it.Amount_expenses_table)              AS `Estimated`
    , null                                      AS `Actual`
FROM bacci.expenses_table it
WHERE it.Date_expenses_table >= '2017-01-01'
        AND it.Date_expenses_table  < '2017-01-01' + INTERVAL 1 YEAR
        and it.Type_expenses_table = 'vehicle rent'
GROUP
     BY YEAR(it.Date_expenses_table)
         union all
SELECT estimated_type                         AS`y_`
    ,null                                     AS `Estimated`
    , sum(estimated_amount)                   AS `Actual`
FROM bacci.estimated_expenses_table it
WHERE it.estimated_year = '2017'
    and it.estimated_type = 'vehicle fuel'
GROUP
     BY YEAR(it.estimated_year)     
         union all
SELECT Type_expenses_table                      AS`y_`
    ,SUM(it.Amount_expenses_table)              AS `Estimated`
    , null                                      AS `Actual`
FROM bacci.expenses_table it
WHERE it.Date_expenses_table >= '2017-01-01'
        AND it.Date_expenses_table  < '2017-01-01' + INTERVAL 1 YEAR
        and it.Type_expenses_table = 'vehicle fuel'
GROUP
     BY YEAR(it.Date_expenses_table)
                  union all
SELECT estimated_type                         AS`y_`
    ,null                                     AS `Estimated`
    , sum(estimated_amount)                   AS `Actual`
FROM bacci.estimated_expenses_table it
WHERE it.estimated_year = '2017'
    and it.estimated_type = 'generator fuel'
GROUP
     BY YEAR(it.estimated_year) 
         union all
SELECT Type_expenses_table                      AS`y_`
    ,SUM(it.Amount_expenses_table)              AS `Estimated`
    , null                                      AS `Actual`
FROM bacci.expenses_table it
WHERE it.Date_expenses_table >= '2017-01-01'
        AND it.Date_expenses_table  < '2017-01-01' + INTERVAL 1 YEAR
        and it.Type_expenses_table = 'generator fuel'
GROUP
     BY YEAR(it.Date_expenses_table)
) d
  GROUP
  BY d.y_

ORDER
  BY d.y_;

结果:

enter image description here

我希望工资管理和员工工资 比汽车租赁,车辆燃料和发电机燃料等设备

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用UNION合并这些查询:

SELECT [First query]
UNION ALL 
SELECT 'Equipments', 'Estimated', 'Actual'
UNION ALL
SELECT [Second query]

请注意,在第一个UNION ALL之后选择只会选择第二个查询的标题。你也可以使用UNION(没有全部),因为它看起来不像你有重复。

答案 1 :(得分:0)

如果你有重复的注册表并且不想看到它们,你可以使用UNION ALL。如果你不介意重复的注册表,你可以使用UNION。 这是和示例

SELECT [query_1] UNION ALL SELECT [query_2] UNION ALL SELECT [query_3]