vendredi 27 janvier 2017

Laravel: MYSQL check if child belongs to top level parent with one query

I have table like this:

id   |   user   |   parent_id   |   level   |
---------------------------------------------
1    |  parent1 |       0       |     1
---------------------------------------------
2    |  parent2 |       0       |     1
---------------------------------------------
3    |  parent3 |       1       |     2
--------------------------------------------
4    |  child1  |       2       |     3
--------------------------------------------
5    |  child2  |       4       |     4 

From child2 I would like to check if it belongs to parent1.

An obvious answer would be run a query from each level starting from child2 > check parent > check parent > until it is parent1. But that will be a lot of query to run.

Is there any way to run this with just one query?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire