user1646859
user1646859

Reputation:

Trying to join 2 tables with same column names

I want to join 2 tables but they both have email and password and I want both of the tables to be joined as a new row and not next to each other

So instead of:

c.email         | c.password   | u.email       | u.password
[email protected]  companypass  [email protected]     userpass

I want it to be like

email              | password
[email protected]     companypass
[email protected]        userpass

This is the current query I have which leads to the first example:

SELECT 
      u.email, u.password, c.email, c.password 
FROM 
      korisnici_tbl AS u, companies_tbl AS c

Upvotes: 0

Views: 12635

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

If you'd like to keep rows from both tables, you do not need a join, you need a UNION:

SELECT u.email, u.password FROM korisnici_tbl AS u
UNION ALL
SELECT c.email, c.password FROM companies_tbl AS c

The ALL option will keep duplicates, if any are found in both tables. If you want duplicates removed, drop ALL from the UNION.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

If you want them to be in different rows, you can use a UNION (or UNION ALL, which allows duplicate rows)

SELECT email, password FROM companies_tbl
UNION ALL SELECT email, password FROM korisnici_tbl

Upvotes: 3

sgeddes
sgeddes

Reputation: 62831

One option is to use a UNION:

SELECT email, password 
FROM korisnici_tbl 
UNION
SELECT email, password
FROM companies_tbl

If you want potential duplicate rows, use UNION ALL instead.

Upvotes: 1

Related Questions