I think I’ve been looking at this too long and have massively overcomplicated it.
I have two tables, TITLES and RANKS. I want to get all TITLES‘s which share any RANK‘s associated to a given ID, tables and expected output below:
+------+------+
|ID |TITLE |
+------+------+
|1 |A |
+------+------+
|2 |B |
+------+------+
|3 |C |
+------+------+
|4 |D |
+------+------+
|5 |E |
+------+------+
+------+------+------+
|USERID|ID |RANK |
+------+------+------+
|1 |1 |4 |
+------+------+------+
|2 |2 |2 |
+------+------+------+
|3 |2 |4 |
+------+------+------+
|4 |3 |2 |
+------+------+------+
|5 |3 |5 |
+------+------+------+
Expected output querying on ID 2 :
+------+
|ID |
+------+
|A |
+------+
|B |
+------+
|C |
+------+
>Solution :
It sounds like you are asking for just:
select distinct t1.TITLE
from table2 t2id
join table2 t2rank on t2rank.rank=t2id.rank
join table1 on t1.ID=t2rank.ID
where t2id.ID=2