2017-07-27 50 views
1
select * 
from Table1 
full join Table2 on Table2.Common = Table1.Common 

在上面的查詢,我想補充這where條件:充分參與和where條件

where (Table1.StatusId > 100 or Table1.StatusId is not null) 

StatusId是數據類型numeric(18, 2)的。

問題是當我使用where條件時,Table2中沒有任何連接數據的行在Table1中不再顯示。

有沒有其他方法可以添加此where條件?

+0

您正在檢查WHERE子句中的Table1的字段,並且您期望Table1中的那些空值將被顯示?也許你需要條件的第二部分,如:「或Table1.StatusId爲空」 –

+1

其中(Table1.StatusId> 100或Table1.StatusId不爲空)'沒有任何意義。它相當於'where Table1.StatusId不爲空'。 –

回答

2

儘量把它放在一個Subquery

SELECT * 
FROM (SELECT * 
      FROM  Table1 
      WHERE  (Table1.StatusId > 100 
         OR Table1.StatusId IS NOT NULL 
        ) 
     ) T 
     FULL JOIN Table2 ON Table2.Common = t.Common 
+0

謝謝。這一個工作。謝謝大家給我解決方案和幫助我:) – Ashin

0

你可以只添加一個AND (Table1.StatusId > 100 or Table1.StatusId is not null)您的ON-聲明後

0
select * 
from Table2 
Full join Table1 on Table2.Common = Table1.Common 
where (Table2.StatusId > 100 or Table2.StatusId is not null) 
+0

如果WHERE子句中現有的「Table1.StatusId不爲空」條件(如問題中所述),使用的連接並不重要,因爲它將等同於INNER JOIN。 –

+0

有3種情況。數據僅在table1中;表1和表2中的數據,僅表2中的數據;這就是爲什麼我使用完全加入。我必須添加table1的條件。所以對於上面的情況3,如果table1中沒有數據,數據不會從表2中獲取 – Ashin

+0

我編輯了我的答案,現在有幫助嗎 – Ashu

1

移動t1.StatusId is NOT NULL過濾器ON條件

SELECT * 
FROM Table1 t1 
     FULL JOIN Table2 t2 
       ON t1.Common = t1.Common 
       AND t1.StatusId is NOT NULL 

在您的查詢的問題是,在table1狀態欄的非匹配記錄NULL值將出現,這將被過濾通過應用在哪裏條件Where t1.StatusId is NOT NULL。因此,它移動到ON條款,告訴什麼是加盟的,而不是過濾結果

0

的記錄如果你想在這裏的條件從表1適用於連接條件的結果,那麼

SELECT * 滿加入表2上Table2.Common = Table1.Common其中Table1.StatusId> 100或Table1.StatusId不是null

這個結果可能不會有從表2,其具有表1從由於沒有連接的數據的數據你的狀況(Table1.StatusId不爲空)。

如果你想加入到調理表1中只篩選的記錄將被執行,然後

SELECT * FROM表2全連接(SELECT * FROM表1,其中StatusId> 100或StatusId不爲null)T於Table2.Common = T.Common