Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mysql> describe a;
- +-----------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------+-------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | parent_id | int(11) | YES | MUL | NULL | |
- | name | varchar(45) | NO | | NULL | |
- +-----------+-------------+------+-----+---------+----------------+
- 3 rows in set (0.04 sec)
- mysql> set @t=5; select distinct b.node_id, a.* from a, (select @t:=if(a.id = @t, a.parent_id, @t) as node_id from a order by a.id DESC) as b where a.id=b.node_id;
- Query OK, 0 rows affected (0.00 sec)
- +---------+----+-----------+------+
- | node_id | id | parent_id | name |
- +---------+----+-----------+------+
- | 5 | 5 | 4 | L3_0 |
- | 4 | 4 | 2 | L2_0 |
- | 2 | 2 | 1 | L1_0 |
- | 1 | 1 | NULL | ROOT |
- +---------+----+-----------+------+
- 4 rows in set (0.02 sec)
Advertisement
Add Comment
Please, Sign In to add comment