4
如何簡化下面的SQL查詢,如何簡化下面的SQL查詢?
DECLARE @EMPLOYEE1 TABLE (EMPID INT,DEPT1 INT,DEPT2 INT)
DECLARE @EMPLOYEE2 TABLE (EMPID INT,DEPT1 INT,DEPT2 INT)
INSERT INTO @EMPLOYEE1 VALUES
(1,1,1),
(2,2,2),
(3,10,3),
(4,4,4)
INSERT INTO @EMPLOYEE2 VALUES
(1,1,1),
(2,2,2),
(3,10,10),
(4,10,4)
SELECT A.EMPID,
A.DEPT1 EMP1_DEPT,
0 TYPES
FROM @EMPLOYEE1 A
LEFT JOIN @EMPLOYEE2 B ON A.DEPT1=B.DEPT1
WHERE B.DEPT1 IS NULL
UNION ALL
SELECT A.EMPID,
A.DEPT2 EMP2_DEPT,
1 TYPES
FROM @EMPLOYEE1 A
LEFT JOIN @EMPLOYEE2 B ON A.DEPT2=B.DEPT2
WHERE B.DEPT2 IS NULL
任何一個可以排序這個問題,在此先感謝
你會如何看待輸出? – Hexxed
您的當前查詢在任何'JOIN' /'WHERE'條件下是不是嘗試使用'EMPID'是否正確? –