Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNION with dissimilar columns

Tags:

sql-server

TABLE A

ID    Name    Age
1     John       22

TABLE B

ID   Name
5    Erik

I want result like

ID    Name    Age
1     John    22
5     Erik    

When I am performing UNION giving error

"All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists."

How to get desired result?

like image 848
Yogesh Avatar asked Oct 17 '11 09:10

Yogesh


1 Answers

You could supply a dummy column in lieu of the missing one that returns NULL as below.

SELECT ID,
       Name,
       Age
FROM   TABLE_A
UNION ALL
SELECT ID,
       Name,
       NULL
FROM   TABLE_B  
like image 187
Martin Smith Avatar answered Sep 29 '22 01:09

Martin Smith