Nuvolari
Nuvolari

Reputation: 1155

MySQL Multiple Where Clauses Using One Table

I have a table like this:

user_id, name, second_name

Sample data:

1, "fred", "smith"
2, "john", "smith"
3, "karl", "johnson"

I want to find all the people that have a name of say "fred". Then I want to find all the people that have the same second name as "Fred". I would like to do this in one query if possible. A query simialar to this:

SELECT * FROM people WHERE name=:first_name OR second_name=:result_from_first_row

The output of this sample query would be:

1, "fred", "smith"
2, "john", "smith"

Upvotes: 0

Views: 244

Answers (1)

Felix Ebert
Felix Ebert

Reputation: 1161

SELECT * FROM people WHERE second_name IN (SELECT second_name FROM people WHERE name=:first_name)

Upvotes: 1

Related Questions