Top parent hierarchy search

Hello,

I have a table of users and their parents.If the parent is 0, this parent is the top parent of a group. I want to calculate to have for each user, his/her top parent.

Example:

user | parent

1 | 3

2 | 0

3 | 4

4 | 5

5 | 0

6 | 2

Result

user | top_parent

1 | 5

2 | 2

3 | 5

4 | 5

5 | 5

6 | 2

Do you have any suggestion?

Thank you!