Update on SQL join
I have two tables. First one has empid and managerid, whereas the 2nd table has empid as well as other columns. However the table2 doesn't have any tuples. How do I update table2 so that for a particular manager if an empid doesn't exist then it gets added in table2 else other fields of table2 gets updated.
In short, how to update a table without any rows, using join tables.
I am using this command
UPDATE EMPLOYEE_GOAL_TABLE
SET ISGoal1='yes'
FROM EMPLOYEE_TABLE
LEFT OUTER JOIN EMPLOYEE_GOAL_TABLE
ON EMPLOYEE_TABLE.EmployeeID = EMPLOYEE_GOAL_TABLE.EmployeeID
WHERE EMPLOYEE开发者_C百科_GOAL_TABLE.EmployeeID IS null.
but this shows no rows affected.
If you want to add missing records in EMPLOYEE_GOAL_TABLE
where a record exists in EMPLOYEE_TABLE
then you need to do an INSERT
and not an UPDATE
on EMPLOYEE_GOAL_TABLE
. You can not update a record in EMPLOYEE_GOAL_TABLE
if it does not exist. (I hope I am understanding your requirement correctly. :) )
So if the goal is to add missing records then do something like this...
INSERT INTO EMPLOYEE_GOAL_TABLE (EmployeeID, ISGoal1)
SELECT EMPLOYEE_TABLE.EmployeeID, 'yes'
FROM EMPLOYEE_TABLE
WHERE EMPLOYEE_TABLE.EmployeeID NOT IN
(
SELECT DISTINCT EMPLOYEE_GOAL_TABLE.EmployeeID
FROM EMPLOYEE_GOAL_TABLE
)
is your base query returning any records?
what do you get when you
SELECT *
FROM EMPLOYEE_TABLE
LEFT OUTER JOIN EMPLOYEE_GOAL_TABLE
ON EMPLOYEE_TABLE.EmployeeID = EMPLOYEE_GOAL_TABLE.EmployeeID
WHERE EMPLOYEE_GOAL_TABLE.EmployeeID IS null
?
精彩评论