如何在JOIN中显示数据,并返回空值?

时间:2016-09-09 09:04:02

标签: sql sql-server join

如果另一个表中的值不可用,如何连接两个表并返回 null 值?

我试图加入我发现的很多例子,但是我很难得到希望。谢谢!

Screenshoot

2 个答案:

答案 0 :(得分:0)

尝试 LEFT OUTER JOIN

SELECT *
FROM Table1 t             -- Specify the table whose value you need to display even when no match is found
LEFT OUTER JOIN Table2 t2
ON t.id=t2.t_id

答案 1 :(得分:0)

使用 LEFT JOIN

SELECT
    VA.COL_PERSONNEL_NUMBER,
    VA.COL_CLOCK_DATE,
    VA.P10,
    VA.P20,
    VR.NAME,
    VR.UNIT
FROM VIEW_ABSEN VA
LEFT JOIN VIEW_REPORT VR ON VA.COL_PERSONNEL_NUMBER=VR.ID
    AND VR.DATE=VA.COL_CLOCK_DATE
WHERE VA.COL_PERSONNEL_NUMBER LIKE '%521663%'
    AND VA.COL_CLOCK_DATE BETWEEN '2016-08-01' AND '2016-08-05'