I’ve become stuck on particular problem of finding common items between tables. I have to works with these 3 tables – People can own have more than one home.
I want to find all people that have owned the same home as “John Doe”
Persons
+------+------------------+----------+
| id | firsname | lastname |
+------+------------------+----------+
| 1 | John | Doe |
+------+------------------+----------+
Home
+------+------------------+-----------+
| h_id | address | year_built|
+------+------------------+-----------+
| 1 | 1233 SQL PL NW | 1995 |
+------+------------------+-----------+
Ownership
+-----------+------------------+
| person_id | house_id |
+-----------+------------------+
| 1 | 1 |
+-----------+------------------+
My current thoughts on what the query should go is that I check Persons.id matches the ownership.id then find the house.id and compare it two a some Person2.
Basically you need to go
This should do it
as dtbarne notes the where clause assume you know the ID. If you want you change the WHERE clause to
The problem there of course is if two records in person share the name you’ll get multiple results so you want to add House information to the
SELECTclause to differentiate