表1)m_conservationsetting表1左連接表2負(連接數據)
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單位類別編號)
表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 2 2
表1需要保留與表2連接,它應該省略連接結果並僅顯示錶1數據作爲結果。 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
爲什麼你使用'LEFT JOIN'如果只想那些在表2中的特定狀態行? – Barmar
如果使用'GROUP BY cs.CategoryId',則不能在結果中得到具有相同'CategoryId'的兩行。 – Barmar
你想得到的結果不僅僅是左連接檢查結果的值,你還有一個值是左連接結果1,1,2 –