Charlie Ansell
Charlie Ansell

Reputation: 461

Check data from multiple tables?

I am trying to check data from different tables, here is the breakdown of what I am trying. Say I have two tables C & D. Both tables have the columns username and password. I am trying to query both of them to see if at least one of them contain the correct username or password. Here is the code I came up with but it doesn't seem to be returning correct results.

SELECT USERNAME
     , PASSWORD 
  FROM D
     , C 
 WHERE D.USERNAME ="HI" 
    OR C.USERNAME="HI" 
   and D.PASSWORD="PASS" 
    OR C.PASSWORD="PASS";

This just returns a blank result list when I know that Table D will contain the username Hi and password pass. Can you guys see anything wrong with my query?

Upvotes: 0

Views: 106

Answers (1)

Nick
Nick

Reputation: 147156

You should be writing this as a UNION, not a JOIN:

SELECT USERNAME,PASSWORD 
FROM D
WHERE D.USERNAME ="HI" AND D.PASSWORD="PASS"
UNION
SELECT USERNAME,PASSWORD 
FROM C
WHERE C.USERNAME ="HI" AND C.PASSWORD="PASS"

Upvotes: 1

Related Questions