加入两张桌子

时间:2015-03-10 13:08:33

标签: sql sql-server

enter image description here

在此表1和表2中(AttendanceDate,EmployeeCode)是PRIMARY Key .. 我们如何用table2替换table1值,其中AttendanceDate和EmployeeCode将匹配..

与结果表一样..

2 个答案:

答案 0 :(得分:2)

尝试这个未经测试的查询:

select t1.AttendanceDate, t1.EmployeeCode, case when t2.duration is null then t1.duration else t2.duration end 
from table1 t1 left outer join table t2 on t1.AttendanceDate= t2.AttendanceDate and  t1.EmployeeCode =  t2.EmployeeCode

答案 1 :(得分:-1)

这应该有用。

SELECT Table1.AtendanceDate, Table1.EmployeeCode,
    CASE WHEN Table2.Duration IS NOT NULL THEN Table2.Duration ELSE Table1.Duration END AS Duration
FROM Table1 
LEFT OUTER JOIN Table2 ON Table1.AtendanceDate = Table2.AtendanceDate 
    AND Table1.EmployeeCOde = Table2.EmployeeCode