求教两个无关联表的右连接
t1表数据
a b c
t2表数据
d e
f g
如何得到结果:
a b c d e
null null null f g
[解决办法]
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')BEGIN DROP TABLE tbaENDGOCREATE TABLE tba( col1 VARCHAR(10), col2 VARCHAR(10), col3 VARCHAR(10))GOINSERT INTO tbaSELECT 'a','b','c'GOIF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tbb')BEGIN DROP TABLE tbbENDGOCREATE TABLE tbb( col1 VARCHAR(10), col2 VARCHAR(10))GOINSERT INTO tbbSELECT 'd','e' UNIONSELECT 'f','g'GOWITH t1 AS(SELECT RN=ROW_NUMBER() OVER (ORDER BY GETDATE()),col1,col2,col3FROM tba),t2 AS(SELECT RN=ROW_NUMBER() OVER (ORDER BY GETDATE()),col1,col2FROM tbb)SELECT t1.col1,t1.col2,t1.col3,t2.col1,t2.col2FROM t1 FULL OUTER JOIN t2 ON t1.RN = t2.RNcol1 col2 col3 col1 col2a b c d eNULL NULL NULL f g