使用join更新表

时间:2016-12-22 04:22:34

标签: mysql sql join

update salaries set diff = a.diff
from attendence a
join salaries s on s.employees_id = a.employees_id
and s.date = a.date where salaries.employees_id = 22 and date = CURDATE()

我在MySQL中使用此代码时遇到问题

  

from不是此位置的有效输入"

4 个答案:

答案 0 :(得分:2)

试试这个

UPDATE attendance a
       JOIN salaries s
       ON s.employees_id = a.employees_id AND s.date = a.date
       SET s.diff = a.diff
       WHERE salaries.employees_id = 22 AND date = CURDATE()

希望这有效。询问是否有任何疑问。

答案 1 :(得分:0)

试试这个

Update salaries
Set diff = (
    Select a.diff
    From attendence a
    Join salaries s On s.employees_id = a.employees_id and s.date = a.date
    Where salaries.employees_id = 22 And date = Curdate()
)

还要确保选择查询没有错误并且只返回一行。

答案 2 :(得分:0)

您正在使用Microsoft SQL Server的语法。 MySQL使用不同的语法。两者都是非标准的,因为多表UPDATE不是标准SQL规范的一部分。

在MySQL中你应该使用这样的语法:

update attendence a join salaries s 
  on s.employees_id = a.employees_id and s.date = a.date
set s.diff = a.diff 
where s.employees_id = 22 and s.date = CURDATE();

您应该通过阅读MySQL reference documentation for UPDATE Syntax来自行解答。

答案 3 :(得分:0)

    update sa  
    set diff = a.diff
    from salaries sa 
    join  attendence a
     on sa.employees_id = a.employees_id
    and sa.date = a.date
     where sa.employees_id = 22 and sa.date = CURDATE()

simple e.g below

UPDATE A
SET foo = B.bar
FROM TableA A
JOIN TableB B
    ON A.col1 = B.colx
WHERE ...