uwe
uwe

Reputation: 4097

Find all user in table A that are also in Table B

I have table A with fields user1 and user2 and table B with user3 and user4 pairs.

I want to be able to find any rows from table A where the user1/user2 combination is also in table B.

The order doesn't matter. For example table A with user1=Mike, user2=Joe would match table B with user3=Joe and user4=Mike.

Upvotes: 0

Views: 52

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490338

I'd probably just use an explicit or in a join:

select user1, user2 
      from tableA join tableB on 
          (user1=user3 and user2=user4) or 
          (user1=user4 and user2=user3)

...but take it with a grain of salt. I've been accused of over-using joins, probably with at least a little reason.

Upvotes: 4

James Youngman
James Youngman

Reputation: 3733

select a.user1, a.user2 from a, b 
where (a.user1 == b.user3 and a.user2 == b.user4)
or (a.user1 = b.user4 and a.user2 = b.user3);

Upvotes: 1

Related Questions