I have a table like this:
| id | user_id | referral_id |
|---|---|---|
| 1 | 1 | null |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
| 4 | 4 | 1 |
| 5 | 5 | 3 |
When I query where user_id = 5, I would like to get the following:
| id | user_id | referral_id |
|---|---|---|
| 1 | 1 | null |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
| 5 | 5 | 3 |
I'm using MySQL 5 so I can't use cte. Edit: I need the parents for the id not the children.