2015-07-03 17 views
0

表1)
m_conservationsettingLeftJoin減號(右表數據+內連接數據)

+------------+-------+------------+ 
| FacilityId | Unit | CategoryId | 
+------------+-------+------------+ 
|  1  | 1 |  1  | 
|  1  | 1 |  2  | 
|  1  | 1 |  3  | 
|  1  | 2 |  1  | 
|  1  | 2 |  2  | 
|  2  | 1 |  1  | 
|  2  | 2 |  1  | 
+------------+-------+------------+ 

唯一密鑰(FacilityId單位的CategoryId)

表2)的
l_maintelog

+------------+------+------------+--------+ 
| FacilityId | Unit | CategoryId | Status | 
+------------+------+------------+--------+ 
| 1  | 1 |  1  | 0 | 
| 1  | 1 |  2  | 1 | 
| 1  | 1 |  3  | 0 | 
| 1  | 2 |  1  | 0 | 
| 2  | 1 |  1  | 0 | 
| 2  | 2 |  1  | 0 | 
+------------+------+------------+--------+ 

結果:

+------------+------+------------+ 
| FacilityId | Unit | CategoryId | 
+------------+------+------------+ 
|  1  | 1 |  2  | 
|  1  | 2 |  2  | 
+------------+------+------------+ 

表1需要被留下了表2加入,它應該ommit連接結果,僅顯示table1的數據結果。
Table1 LeftJoin Table2 - (join Data)for the following query。用於獲取結果的條件是檢查狀態= 0備案表2中

查詢

select cs.FacilityId,Cs.Unit,cs.CategoryId 
from m_conservationsetting cs 
LEFT JOIN l_maintelog ml on 
(cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit) 
WHERE (ml.Status=0 
) 
group by cs.CategoryId 
+3

嘗試編輯您的問題,並提供樣本數據和預期結果。 「[LeftJoin - (右表數據+內連接數據)]」對我來說並不意味着什麼(至少)。 –

+0

@Gordon我只需要左表數據省略內部聯接數據。 – neo

+0

。 。讓我重複一遍:編輯問題幷包括樣本數據和期望的結果。 –

回答

0

您可以使用第二個表的鍵列NULL條件,以消除結果是有兩個表中

Select cs.FacilityId,Cs.Unit,cs.CategoryId 
from m_conservationsetting cs 
LEFT JOIN l_maintelog ml on 
(cs.FacilityId=ml.FacilityId and cs.Unit=ml.Unit) 
WHERE (ml.Status=0 
and ml.FacilityId IS NULL) 
group by cs.CategoryId