Union two tables with different number of columns
P粉879517403
2023-08-24 18:54:05
<p>I have two tables (Table A and Table B). </p>
<p>The two tables have different number of columns - assuming table A has more columns. </p>
<p>How can I merge these two tables and get null values on columns that table B doesn't have? </p>
I came here and followed the answer above. But the order mismatch of data types resulted in an error. The description below from another answer will be helpful.
Are the above results the same as the column order in your table? Because Oracle is very strict on column order. The following example will generate an error:
ORA-01790: expression must have the same data type as the corresponding expression
As you can see, the root cause of the error is a column order mismatch implied by using * as the column list specifier. This type of error can be easily avoided by entering the column list explicitly:
select col_a, col_b, col_c from test1_1790 union all select col_a, col_b, col_c from test2_1790; A more common situation for this error is when you inadvertently swap (or move) two or more columns in a SELECT list:
Alternatively, if the above doesn't solve your problem, how about creating an alias in the column , like this: (The query is different from yours, but the point here is how to create an alias in the column Add an alias in.)
For tables with fewer columns, you can add additional empty columns, for example: