select batchcode,qc,l,c from
(select * from 表1 a join 表2 b on a.batchcode=b.batchcode) ab
join 表3 c on ab.batchcode=c.batchcode
full join 将三个表连起来不可以吗?
使用左或者右连接就行,left join 或者 right join
-------------------------------
-- 你好,
-- 我的答案如下(性能自己注意了)
-------------------------------
SELECT 'KE20GBD-015510802' AS batchcode, 1 AS qc INTO #tem_1
SELECT 'KF14GBD-015760709' AS batchcode, 100 AS l INTO #tem_2 UNION ALL
SELECT 'KF24GBC', 23 UNION ALL
SELECT 'KH20GBD-015510802', 10
SELECT 'KE20GBD-015510802' AS batchcode, 1 AS c INTO #tem_3 UNION ALL
SELECT 'KF14GBD-015760709', 100 UNION ALL
SELECT 'KF24GBC', 23
SELECT * FROM #tem_1
SELECT * FROM #tem_2
SELECT * FROM #tem_3
/*
DROP TABLE #tem_1
DROP TABLE #tem_2
DROP TABLE #tem_3
*/
SELECT
CASE
WHEN t1.batchcode IS NULL THEN CASE
when t2.batchcode IS NULL THEN t3.batchcode
ELSE t2.batchcode
END
ELSE t1.batchcode
END batchcode,
--t1.batchcode,
--t2.batchcode,
--t3.batchcode,
t1.qc,
t2.l,
t3.c
FROM
#tem_1 t1 FULL JOIN #tem_2 t2
ON t1.batchcode = t2.batchcode
FULL JOIN #tem_3 t3
ON t1.batchcode = t3.batchcode
or t2.batchcode = t3.batchcode