简体   繁体   中英

Update table using 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()

I have a problem using this code in MySQL

from is not a valid input at this position"

Try this

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()

Hope this works. Ask if any doubt.

Try this

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()
)

Also make sure that select query has no error and returns only one row.

You're using the syntax of Microsoft SQL Server. MySQL uses a different syntax. Both are non-standard, because multi-table UPDATE is not part of the standard SQL specification.

In MySQL you should use syntax like this:

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();

You should have answered this on your own by reading the MySQL reference documentation for UPDATE Syntax .

    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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM