在SQL server中,我有以下表格(它们是简化的):
表A:
ID| NAME | ID_STATUS | STATUS
--------------------------------
1 | One | 1 | Confirmed
2 | Two | 2 | Pending
3 | Three| 0 | Deleted
4 | Four | 1 | Confirmed
表B(详见A):
ID (same from A) | NAME | ID_STATUS | STATUS
-----------------------------------------------
1 | One | 1 | Confirmed
1 | One | 1 | Confirmed
2 | Two | 2 | Pending
3 | Three| 2 | Pending
3 | Three| 2 | Pending
4 | Four | 2 | Pending
我需要从A获取不同的行(ID,NAME,ID_STATUS,STATUS),其中(ID_STATUS,STATUS)与B不同
预期结果:
ID | NAME | ID_STATUS | STATUS
---------------------------------
3 | Three| 0 | Deleted
4 | Four | 1 | Confirmed
我试图通过LEFT OUTER JOIN尝试没有成功...(返回0结果)
select A.ID, A.NAME, A.ID_STATUS, A.STATUS from A left outer join B
on A.ID = B.ID
where
A.ID_STATUS != B.ID_STATUS
答案 0 :(得分:0)
select A.ID, A.NAME, A.ID_STATUS, A.STATUS
from A
left outer join B
on A.ID = B.ID
and A.ID_STATUS = B.ID_STATUS
where b.ID is null
答案 1 :(得分:0)
选择A.ID,A.NAME,A.ID_STATUS,A.Acol来自A左外连接B A.ID = B.ID和A.ID_STATUS = B.ID_STATUS其中B.ID为空;