For context, look here:
Database Model to Represent Families, Households, and Relationships
Basically, I am trying to map relationships between people and households without tying relationships and households to a single "head of household". So far I think my model works well. Here's what works:
SELECT person.person_id,person.first_name, relationship.person_id_a, relationship.person_id_b, relationship.relationship_cd
FROM person, relationship
WHERE person_id = 1
AND (person.person_id = relationship.person_id_a
OR person.person_id = relationship.person_id_b)
AND relationship.relationship_cd <> 0;
results in:
"person_id" "first_name" "person_id_a" "person_id_b" "relationship_cd"
"1" "Joe" "1" "2" "8 - Spouse"
"1" "Joe" "1" "3" "7 - Sibling"
So I know that Joe (person 1) has a spouse (person 2) and a sibling (person 3). Is there a way for me to use attach the name (or other info) of the people Joe has a relationship with to the output? That is, query 1 tells me who Joe has relationships with (from a join with the relationship table), can I nest another query in such a way that I can then pull the name of that person (from a join back to another record from the person table)? So that it looks like this:
"person_id" "first_name" "person_id_b" "relationship_cd" "related_person_name"
"1" "Joe" "2" "8 - Spouse" "Susan"
"1" "Joe" "3" "7 - Sibling" "Bob"