Hunter
Hunter

Reputation: 158

PostreSQL - Query 2 tables for something that exists in only one and return the row from the table it existed in

I want to query 2 tables for an email (which will only exist in one of the tables) and return the row from the table where the email exists.

       Table a                             Table b
id | email | password     id | email | password | more | things
 1    foo       1          8    bar        a        q       d

I'd like to be able to query for 'bar' in both tables and only have it return the row from Table b. Likewise the same query with 'foo' should return just the row from Table a.

Is this possible? I had tried several things, but most recently the following and it returns everything from both tables.

SELECT *
FROM a, b
WHERE a.email = 'foo'
OR b.email = 'foo';

Upvotes: 0

Views: 15

Answers (1)

Hunter
Hunter

Reputation: 158

I figured it out! Woo! In case anyone else needs help with a similar issue in the future I queried using the below syntax.

SELECT a.email, a.password
FROM a
WHERE a.email = 'foo'
UNION ALL
SELECT b.email, b.password
FROM b
WHERE b.email = 'foo'
;

Upvotes: 1

Related Questions