Well, I have 2 tables like this:
Table1
ID | USER_ID
1 0
2 2
3 15
4 16
Table2
ID | FROM | TO
9 0 2
9 2 16
9 16 15
9 15 0
10 15 2
What I want is really simple but driving me crazy, considering that ID , FROM and TO represents users in table 2. I want to get someone in FROM (which is Table1.user_id) with an ID in table2 such as it also exists in TO (which is the same Table1.user_id) with the same ID of table2
For example, record 16 is eligible. Because it appears in From with ID of 9 and as TO with the same ID of 9 in table 2 (both TO and FROM correspond to a user_id of 15 in table1)
What I have done was:
select *
from `Table1`
where exists (select ID from `Table2` as p1 where FROM = 16)
and exists (select ID from `Table2` as p2 where ID = 16)
and p1.ID = p2.ID