简体   繁体   中英

how to append two tables with different columns in aws athena

I want help in writing a query which appends two tables with some similar columns and other columns too. For example if I have 2 tables,

Table 1:

   name    age    place
    n1      a1       p1
    n2      a2       p2
    n3      a3       p3

Table 2:

   name    place   country
    n4       p4      c4
    n5       p5      c5
    n6       p6      c6

I want to append these two tables to get

Table 3:

  name    age   place   country
    n1      a1     p1       NULL   
    n2      a2     p2       NULL
    n3      a3     p3       NULL
    n4      NULL   p4       c4     
    n5      NULL   p5       c5
    n6      NULL   p6       c6

Is this possible? Thanks

You can use a UNION query with placeholders for missing columns on each table:

SELECT name, age, place, null AS country FROM table1
UNION ALL
SELECT name, null AS age, place, country FROM table2
ORDER BY name;

Notice that I have created two columns so that each result set has the same set of columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM