How to build "empty columns" for UNION?

from what I understand, every SELECT statement in UNION should have the same number of columns. Columns must also have similar data types. In addition, the columns in each SELECT statement must be in the same order. "Well, if the first SELECT has more columns than the second that can generate. Here is what I mean: suppose I want

SELECT "City", "Country", "Continent" from table1  
UNION  
SELECT "City", "Country" from table2     

... let's say that table 2 does not contain a column called "Continent", but for my needs it is good for the records that come from table2 to be empty or NULL in this column. I am using dashDB.

+4
source share
2 answers

You can always add “virtual” columns:

SELECT "City", "Country", "Continent" from table1  
UNION  
SELECT "City", "Country", NULL AS "Continent" from table2 
+8


,

 SELECT "City", "Country", "Continent" from table1  
UNION  
SELECT "City", "Country", ' ' as "Continent"  from table2

 SELECT "City", "Country", "Continent" from table1  
    UNION  
    SELECT 

"City", "Country", NULL as "Continent"  from table2

"" 2

+2

Source: https://habr.com/ru/post/1656196/


All Articles