如何使用外键更新表中整个列的值相对于另一个表中的列的值:MYSQL

时间:2016-02-08 07:25:37

标签: mysql

我有一个名为hours_worked的表,它有两列,employeeID(主键)和working_hours_for_a_month。我有另一个表工资,它有两列属性employeeID(外键为hours_worked),工资。现在我想通过匹配hours_worked和salary表中的employeeID来更新salary_worked(working_hours_of_a_month)* 150中的值的salary属性列。我想在一次罢工中更新整个专栏。可能的?

2 个答案:

答案 0 :(得分:1)

update salary s 
    left join hours_worked hw on hw.employeeID = s.employeeID 
set 
    s.salary = hw.worked_hours_for_a_month*150
欢呼:)

答案 1 :(得分:0)

您可以使用此SQL查询

UPDATE salary s
LEFT JOIN hours_worked hw ON
    hw.employeeID = s.employeeID
SET
    s.salary = hw.worked_hours_of_a_month * 150

以下是How to update table's column value based on another table's column value

的参考资料