For school I've got the following assignment to UPDATE data into a database:
Give all the employees (located into the employee table) who work on a project where the employee "50" (employee code) works on as well a salary raise of 250 euro
I know I had to get the SAL column in the employee and then raise it by 250 euro. I currently did this by the following query.
UPDATE `work` AS work
INNER JOIN `employee` AS employee ON employee.`CODE` = work.`W_CODE`
SET employee.`SAL` = (employee.`SAL` + 250)
WHERE work.`P_CODE` IN ( # .... );
The P_CODE column stands for the project identifier. In the work table, all employee's with the current project they are working on are listed.
For getting the projects where the employee with the code 50 worked on, I made the following subquery:
UPDATE `work` AS work
INNER JOIN `employee` AS employee ON employee.`CODE` = work.`W_CODE`
SET employee.`SAL` = (employee.`SAL` + 250)
WHERE work.`P_CODE` IN (SELECT work.`P_CODE`
FROM `work` AS work
INNER JOIN `employee` AS employee ON `employee`.`CODE` = work.`W_CODE`
WHERE employee.`CODE` = "50");
When running this query, I get this error:
Error Code: 1093. Table 'work' is specified twice, both as a target for 'UPDATE' and as a separate source for data
For my own try, I did some research and found I can't use the same table twice for this.
Questions
How can I fix this query where the sub-query will work? Can it be combined in my first join?