李韋德
李韋德

Reputation: 23

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"

Upvotes: 2

Views: 77

Answers (4)

Abhay Verma
Abhay Verma

Reputation: 1

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.

Upvotes: 0

Nishant Saini
Nishant Saini

Reputation: 431

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

Upvotes: 0

Bill Karwin
Bill Karwin

Reputation: 562328

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.

Upvotes: 0

kapilpatwa93
kapilpatwa93

Reputation: 4411

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.

Upvotes: 2

Related Questions