mysql 如何在My SQL左连接中对两列求和

u3r8eeie  于 2022-11-21  发布在  Mysql
关注(0)|答案(2)|浏览(222)

所以我有一个表Project

---------------------------------
project_id   |     name| 
---------------------------------
     1            | project 1  | 
     2            | project 2  | 
---------------------------------

表02报告

----------------------------------------------------
report_id    |     report_emp       | report_hours |report_project_id|   
---------------------------------------------------
  1          |         1            |       5    |        1        | 
  2          |         2            |       7    |        1        | 
  3          |         1            |       9    |        2        | 
  4          |         2            |       6    |        1        | 
  5          |         3            |       8    |        2        | 
--------------------------------------------------

和表03 emp

----------------------------------------------
emp_id       |  emp_hourly_cost   |      name  | 
-----------------------------------------------
    1        |         8.5        |       A    | 
    2        |         10         |       B    | 
    3        |         12         |       C    | 
    4        |         9          |       D    | 
    5        |         7.5        |       D    | 
-----------------------------------------------

最后的结果是我想得到这张table

---------------------------------------------
     project     |    hours    |  total cost | 
---------------------------------------------
       1         |      18     |       172.5    | 
       2         |      17     |       172.5    | 
---------------------------------------------

到目前为止,我只得到了总小时,但不是总成本在这个声明项目

SELECT * from projects left join(select sum(report_hours ) as hours , daily_reports_project_id 
     from project_id group by report_id)x on x.report_project_id= projects.project_id;

总成本将是所有的总和〉[表2中emp项目的总时数 * 表3中emp_hourly_cost]
谢谢你的帮助。

j0pj023g

j0pj023g1#

尝试将此SELECT总和(column_name1 + column_name2)作为'Total'

  • 此行从table 1中选择column_name1和column_name2的总和,并为其提供别名“Total”

FROM表格1

  • 此行指定我们需要table 1中的数据

左联接表2

  • 此行使用LEFT JOIN连接table 1和table 2。这意味着将包括table 1中的所有数据,即使table 2中没有匹配的数据

ON表1.列名称1 =表2.列名称2;

  • 此行指定连接的条件。在本例中,它表示table 1中的column_name1应与table 2中的column_name2匹配
SELECT sum(column_name1 + column_name2) as 'Total'
FROM table1
LEFT JOIN table2
ON table1.column_name1 = table2.column_name2;
xzv2uavs

xzv2uavs2#

联接表并聚合:

SELECT p.project_id project,
       SUM(r.report_hours) hours,
       SUM(r.report_hours * e.emp_hourly_cost) total_cost
FROM project p
LEFT JOIN report r ON r.report_project_id = p.project_id
LEFT JOIN emp e ON e.emp_id = r.report_emp
GROUP BY project_id;

如果存在没有报告的项目,并且您希望获得0而不是NULL作为结果,则也可以使用COALESCE()

SELECT p.project_id project,
       COALESCE(SUM(r.report_hours), 0) hours,
       COALESCE(SUM(r.report_hours * e.emp_hourly_cost), 0) total_cost
FROM project p
LEFT JOIN report r ON r.report_project_id = p.project_id
LEFT JOIN emp e ON e.emp_id = r.report_emp
GROUP BY project_id;

请参阅demo

相关问题