Sum totals of two queries

后端 未结 2 1922
耶瑟儿~
耶瑟儿~ 2021-01-01 23:55

I have two basic queries which I need to sum the totals of:

Select hours, sum(hours) FROM table WHERE name=\'xxx\' and Description=\'Worked\'
Select hours2,         


        
2条回答
  •  清酒与你
    2021-01-02 00:22

    First of all, you missed group by, so even though mysql doesn't complain about it, you hours and hours2 values are meaningless. Secondly, you the result of UNION can be put in derived subquery, so you will have the desired total :

    SELECT SUM(hr) FROM
    (
      Select sum(hours) as hr FROM table WHERE name='xxx' and Description='Worked'
      UNION ALL
      Select sum(hours2) as hr FROM table WHERE name='xxx' and Description2='Worked'
    )a
    

提交回复
热议问题