View Full Version : UNION Query


Gannet
10-12-2007, 11:49 AM
I have two tables with data and I want to join the data together for a report.

Example:

SELECT tbl1.a tbl1.b tbl1.c tbl1.d
FROM tbl1
UNION SELECT tbl2.a tbl2.b tbl2.c
FROM tbl2;

Currently I can't join them b/c the column counts aren't the same.
If table1 has more columns than table2 can I join them by indicating some kind of phantom column 'd' for table2 to be joined to column 'd' of table1?

pbaldy
10-12-2007, 12:55 PM
Sure:

SELECT tbl1.a, tbl1.b, tbl1.c, tbl1.d
FROM tbl1
UNION
SELECT tbl2.a, tbl2.b, tbl2.c, "None" As d
FROM tbl2;

Gannet
10-12-2007, 02:15 PM
Worked great. I changed "None" to "" but works now. Thanks pbaldy!

pbaldy
10-12-2007, 02:47 PM
No problemo. It can be anything really; I make it a zero when the field in the other table is numeric, just to keep them consistent.