I have a db table called foo. It has these fields:
- id
- name
- parentId
It can go down 3 levels deep, like this:
- Foo 1 (parentId : 0)
- Foo 2 (parentId: 1) (foo's child 1)
- Foo 3 (parentId: 2) (both foo1 and foo2's child)
- Foo 2 (parentId: 1) (foo's child 1)
Now the problem is, in my users table, there's a field called fooId
. I want to get all the users that belong to a top level foo.
E.g, if a user's fooId is 3, and I want to get all users related to fooId 1 (from the above example), then that user should be counted within the results.
If it was just two levels deep, I could do something like this:
`SELECT stuff FROM users, foo WHERE foo.parentId = '1' AND user.fooId = foo.id
But how can I do it in the present situation?