简体   繁体   中英

I want to update next month employee salary table to current table

Hi I have current salary table callsed jan, columns are employeename, employeeid and Jansalary. I have to update Feb salary to January table. my code is:

update jan 
set jan.salary=feb.salary

from jan
inner join feb
on feb.employeeid=jan.employeeid

my question is if feb have new employee salary data, should I use right join?

Your query is incorrect you can not put UPDATE after FROM it will gives you syntax incorrect.

The correct query is something like this :

update jan, feb
set jan.salary=feb.salary
where feb.employeeid=jan.employeeid;

If you have a new employee salary data on Feb you have two options :

Option 1 : You dont want to have the new employee salary data on Jan so in this case nothing to do because your join is based on employeeid Option 2 : You want to replicate the new employee salary data on Jan so this query can do this :

INSERT INTO `feb` (`employeeid`,`employeename`,`salary`)
SELECT DISTINCT(feb.employeeid), employeename, salary
FROM feb
WHERE feb.employeeid NOT IN (SELECT jan.employeeid FROM jan);

Your design is bad the best is just to create one table called salary and you put the month as filed on this table :

salary table

  1. employeeid
  2. salary_amount
  3. month

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