从多个表中合并列,列不按顺序排列

时间:2017-10-19 14:09:31

标签: sql-server merge union-all

我有3个SELECT FROM,我想将3个结果合并到一个表中,我不能使用一个select * from并使用JOIN来表示3个表。

我可以使用用作Key的A和B列合并表。 我需要在3个合并表上做最后的UNION,其中列的顺序不同。

示例:

from astropy.io import fits
dr7q = fits.open('Shen_dr7_bh_May_2010.fits')
tbdata = dr7q[1].data
w = tbdata[tbdata['SDSS_NAME'] == 'J000006.53+003055.2']

print(tbdata[w])

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT A, B, C, D, E, F, G, H, I
FROM Table 
UNION ALL
(


   SELECT 
   A, 
   B, 
   NULL AS c, 
   NULL AS D,
   NULL AS E,   
   F, 
   G ,
   NULL AS H,
   NULL AS I
   FROM [stuff]


   SELECT 
   A, 
   B, 
   c, 
   NULL AS D,
   E,   
   NULL AS F, 
   NULL AS G ,
   NULL AS H,
   NULL AS I
   FROM [otherStuff]

   UNION ALL

   SELECT 
   A, 
   B, 
   NULL AS c, 
   NULL AS D,
   E,   
   NULL AS F, 
   NULL AS G ,
   H,
   NULL AS I
   FROM [otherstuffbis]


 ) A