Tree example:
ROOT
-c1
-g11
-c2
-c3
-g31
-g32
-c4
-c5
-g51
This tree is stored in a MySQL table built on a nested set model (lft, rgt).
How can I select only the first three children (c1,c2,c3) along with all their descendants? After that, how can I select only the next two children (c4,c5) along with all their descendants?
You have to locate first entity:
SELECT
t1.name AS lev1, IF(t1.name=@sth,'',@sth := t1.name ) lev1_1,
t2.name AS lev2 , IF(t2.name=@sth2,'',@sth2 := t2.name ) lev2_2,
t3.name AS lev3 , IF(t3.name=@sth3,'',@sth3 := t3.name ) lev3_3,
t4.name AS lev4 , IF(t4.name=@sth4,'',@sth4 := t4.name ) lev4_4
FROM
(SELECT @sth:=NULL, @sth2:=NULL, @sth3:=NULL, @sth4:=NULL) AS del,
category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE
t1.name = 'ELECTRONICS';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.