首页 文章

加入两张 table

提问于
浏览
-3

enter image description here

在这个Table1和Table2(AttendanceDate,EmployeeCode)是PRIMARY Key ..我们如何用table2替换table1值,其中AttendanceDate和EmployeeCode将匹配..

像结果表..

2 回答

  • 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

    这应该工作 .

    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
    

相关问题