简体   繁体   English

根据第一个表更新第二个表列

[英]update 2nd table column based on 1st table

I have two tables.我有两张桌子。 One is Employee and other is Department .一个是Employee ,另一个是Department

Employee table has following columns:员工表具有以下列:

Name | Salary | Department Id

Department table has部门表有

Department Id| Department Name | SumofSalary

I want to update SumofSalary department wise我想明智地更新 SumofSalary 部门

Code I have tried我试过的代码

update Department set sumofsalary =
(
    select D.deptid, SUM(E.salary) from Department D inner join Employesalary E 
    on d.deptid=e.deptid group by D.deptid
)

Following will give you sum of salaries department wise and update in department table.以下将为您提供部门明智的工资总额并在部门表中更新。

UPDATE D SET D.SumOfSalary = T.Salary FROM Department D JOIN 
(
    SELECT SUM(Salary) Salary, [Department ID] DeptID FROM Employee Group By [Deptartment ID]
) T ON T.DeptID = D.[Department ID] 

You can try the below code a simple subquery Which will update the sum salary department wise你可以试试下面的代码一个简单的子查询,它将更新工资部门的总和

    UPDATE  dept
    SET dept.sum =h.totalSum  FROM (select  a.deptid, SUM( a.Salary) AS 
    totalSum FROM emp a
    Group By a.deptid) h WHERE 
    dept.deptid=h.deptid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一和第二表进行更新 - Update based of 1st and 2nd table 如果第 2 个表中存在第 1 个表的值,则用第 1 个和第 2 个表的总和更新第 2 个表的值,否则将第一个表数据插入到第 2 个表 - If value of 1 table exists in the 2nd table, update the 2nd table value with sum of 1st and 2nd tables, else insert 1st table data to 2nd table Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 想要查询连接第一个表的第一行和第二个表的前两列 - want query to connect 1st table 1st row and 2nd table first two column 根据第一张表的ID汇总第二张表中的值 - Summing the values from the 2nd table based on ID of the 1st table 将数据从第三张表链接到第一张表和第二张表 - Link data from third table to a 1st and a 2nd table 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table 仅当第 2 个表中不存在记录时,才将第 1 个表中的记录插入到第 2 个表中 - Insert records from 1st table to 2nd table only when the record is not present in the 2nd table 当第二个表中的布尔值为TRUE时,用第一个表覆盖第二个表的结果 - override result of 2nd table with 1st table when boolean is TRUE in 2nd table SQL查询根据第一个表的输入从第二个表中获取字段 - SQL query to get fields from 2nd table based on input from 1st
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM