views:

60

answers:

4

How can I query the results of two equally designed tables?

if table1 contains 1 column with data:

abc

def

hjj

and table2 contains 1 column with data:

uyy

iuu

pol

then i want my query to return

abc

def

hjj

uyy

iuu

pol

but I want to make sure that if I try to do the same task with multiple columns that the associations remain.

+2  A: 
SELECT col FROM t1 UNION SELECT col FROM t2

Union reference.

Zed
+1, that was really fast
KM
+5  A: 
SELECT 

    Column1, Column2, Column3 FROM Table1

UNION

SELECT 

    Column1, Column2, Column5 AS Column3 FROM Table2

ORDER BY 

    Column1

Notice how I do an order by at the end and that Column5 in Table2 is the equivalent of Column3 in Table1. The Order By is of course optional, but allows you to control the order of items from both tables once they are combined.

Nissan Fan
1+ that was fast!
KM
+2  A: 

Use a UNION

SELECT *
FROM TABLE_A
UNION
SELECT *
FROM TABLE_B

UNION will give you all distinct results, as where UNION ALL will give you results combined from the sets.

astander
This didn't work in SQL. I tried it and it's only giving me the results from the first table
Sev
Okay I got it working with the Union All. Thanks for making that suggestion.
Sev
A: 

sev, since union is the solution to what you described and you say that didn't work, perhaps you can provide the code you wrote that didn't work as clearly we are missing part of the picture. Are you positive the second table has the records you want? How do you know for sure?

HLGEM
actually I figured out my problem. only astander had it right (but only part of his description instead of the code). I was using union instead of union all. what i needed was union all.
Sev