Thursday, November 17, 2016

Copy Selected fields from table2 to table1


If we want to set employeeCode in employeeMaster1 from employeeMaster2 for corresponding employeeId 

A join:

UPDATE employeeMaster1 AS t1
  INNER JOIN employeeMaster2 AS t2 ON t1.employeeId = t2.employeeId

SET t1.employeeCode = t2.employeeCode

By Using left join:

UPDATE employeeMaster1 AS t1
  LEFT JOIN employeeMaster2 AS t2 ON t1.employeeId = t2.employeeId
SET t1.employeeCode = t2.employeeCode


A subquery:

UPDATE employeeMaster1
SET employeeCode = (
  SELECT employeeCode
  FROM employeeMaster2
  WHERE employeeId = employeeMaster1.employeeId

)