如果无法加入,则返回NULL

时间:2014-01-10 20:26:05

标签: sql db2

我有两张桌子:

Providers

----------------------------
| ID | NAME                |
----------------------------
| 1  | Dr. Brown           |
----------------------------
| 2  | Dr. Smith           |
----------------------------
| 3  | Nurse Jones         |
----------------------------
| 4  | Stan Lee, NP        |
----------------------------

Provider_Tax_ID
----------------------------
| ID | TAX_ID              |
----------------------------
| 2  | 79465654            |
----------------------------
| 3  | 32613322            |
----------------------------

尝试将Providers与其税号相匹配时,我想提取所有提供商,包括无法加入Provider_Tax_ID的提供商。

所以我的结果如下:

--------------------------------------------------
| ID | NAME                | TAX_ID              |
--------------------------------------------------
| 1  | Dr. Brown           | NULL                |
--------------------------------------------------
| 2  | Dr. Smith           | 79465654            |
--------------------------------------------------
| 3  | Nurse Jones         | 32613322            |
--------------------------------------------------
| 4  | Stan Lee, NP        | NULL                |
--------------------------------------------------

我尝试过这样的事情,但我没有得到任何结果:

SELECT 
    p.ID,
    p.NAME,
    t.TAX_ID
FROM
    providers p
        FULL OUTER JOIN provider_tax_id t on p.ID = t.ID

1 个答案:

答案 0 :(得分:2)

您只需要一个左(外)联接:

SELECT 
    p.ID,
    p.NAME,
    t.TAX_ID
FROM
    providers p
        LEFT OUTER JOIN provider_tax_id t on p.ID = t.ID