Wednesday, 7 August 2013

Get all rows with a matching field in a different row in the same table

Get all rows with a matching field in a different row in the same table

Let's say I have a table like this:
|id|userID|email |website |
--------------------------------------
|1 |user1 |user1@test.com|website.com|
|2 |user2 |user2@test.com|website.com|
|3 |user3 |user3@test.com|website.com|
|4 |user1 |user1@test.com|foo.com |
|5 |user2 |user2@test.com|foo.com |
And I want to get all of the rows where website='website.com' and have a
corresponding row with a matching userID where website='foo.com'
So, in this instance it would return rows 1 and 2.
Any ideas?

No comments:

Post a Comment