I've searched a lot to find my answer, the most related one is Finding all parents in mysql table with single query (Recursive Query)
The answer is great, but I found that this solution is not working when one node has more than one parent. Table like this:
id, node_id, parent_id
1, 1, 2
2, 2, 3 <-----
3, 2, 4 <-----
4, 4, 5
5, 5, 6
6, 6, 7
Notice that the line 2 and line 3, the node which has node_id=2
has more than one parent.
The answer mentioned above will only return parent id of 2, 3, 4
when node_id=1
, which is not true. (Should be 2, 3, 4, 5, 6, 7
)