Erick
Erick

Reputation: 21

MySQL: SELECT 2 tables with different columns name and amount

I need help. I have 2 tables.

Table 1 with 4 column (email, firstname, lastname, status)

Table 2 with 1 column (address)

And I want to query 2 tables with the result like this:

New Table with 5 column (email, firstname, lastname, status, address)

I already tried with JOIN statement like this

SELECT email, firstname, lastname, status FROM table1 LEFT JOIN table2 ON (0)

The result will be just 4 columns from table1.

And if I use RIGHT JOIN, the result will be the same 4 columns from table1, buat all NULL values.

And with INNER JOIN, the result 4 columns from table1 and with none values.

And then I use UNION like this

SELECT email, firstname, lastname, status FROM table1
UNION
SELECT address, '', '', '' FROM table2

The result will be same as LEFT JOIN RESULT.

Am I missing something?

Upvotes: 1

Views: 1075

Answers (2)

Sylca
Sylca

Reputation: 2545

if you have these two tables, you can do it like babooney said or you can make one more table that will have two foreign keys, one from table1 an another from table2.

example: table3(id, id_table1, id_table2)

Upvotes: 0

babooney
babooney

Reputation: 744

If you don't have a relationship between the two tables i don't understand the need of making two separate tables. I suggest you to make a relation between the tables

an example would be

 table1(email, firstname, lastname, addressID), table2(addressID,Address)

Upvotes: 1

Related Questions