tags:

views:

237

answers:

4

I need a query to make a Select over table1 and table2. I want them to appear as a common result, but without any connection there is no value from table 1 that corresponds with any value from table 2. So if a result was from table1 it should contain the values table1.a , table1.b, table1.c if it comes from table2 it should have table2.d, table2.e, table2.f with the a, b, c-values empty.

SELECT * FROM table1, table2 

...doesn't help me because it always makes connections, connects result of table1 and table2 in one result. I need to have them separate, each result its own row.

Is it simple, difficult or impossible?

+2  A: 

As long as the column types are the same, you can use a union. For example:

select a, b, c
from table1
union
select d, e, f
from table2

If they are not the same, you can still do it by creating 'dummy' columns. As long as the type and number of columns is the same, the results will appear as one set of rows.

Michael Todd
thank you all for your help! works perfect
mafka
A: 

Try SELECT * FROM table1 UNION SELECT * FROM table2

gkrogers
Unless I'm mistaken, this will only work if the number of columns in table1 is equal to the number of columns in table2. But it's been a while since I've done UNIONs in MySQL, maybe that's changed.
Tenner
Yes, I think the number and type of columns has to be the same, and possibly the names/aliases as well. But a UNION query is the way to go.
gkrogers
+2  A: 

If I understand your wish select a,b,c,'','','' from table1 union '','','',e,f,g from table2

replace '' with your favorite place holder for empty column a,b,c - column names.

Dani
+2  A: 

You need to use the same alias for the equivalent columns from both tables, like in :

( select a as FIRST_COL
  from table 1 
) 
union 
( select d as FIRST_COL
  from table2 
)
Billy