0

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"
2
  • It needs to be optional and secondly I don't know what the 'second name' is until the query gets Fred. Commented Feb 17, 2013 at 18:08
  • I misread, sorry. Then I don't know the answer, hope someone does. What do you expect to get if there's two or more "fred"? Commented Feb 17, 2013 at 18:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

That gave me an error but this seemed to work: SELECT * FROM people WHERE second_name=(SELECT second_name FROM people WHERE name='fred')
However I don't believe that's optional.
please try it again, i used "IS IN" instead of a simple "IN" accidentally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.