I need help with a query
SELECT CONVERT(@r, DECIMAL) AS _id, ( SELECT @r 开发者_如何学运维:= parentid FROM People WHERE peopleid = _id ) AS ParentID, @l := @l + 1 AS Level FROM ( SELECT @r := 4, @l := 0, @cl := 0 ) vars, People h WHERE @r 0
And it returns
_id ParentID Level -------------------------- 4 3 1 3 1 2 1 0 3and my table name is "People" and records are-
PeopleID LName FName ParentID 1 ABC XYZ 0 2 PQR JKL 1 3 SAM QWE 1 4 SCOTT JIMMY 3
Que:- It is basically correct but only difference is instead of passing in 4(i.e. no of rows) I want to pass in 1 (i.e. starting point) and that result set should return in reverse order.
how it will be done? should i change something in query?Thanks,
KishorSELECT _id, ParentID, Level
FROM (
SELECT CONVERT(@r, DECIMAL) AS _id,
(
SELECT @r := parentid
FROM People
WHERE peopleid = _id
) AS ParentID,
@l := @l + 1 AS Level
FROM (
SELECT @r := 4,
@l := 0,
@cl := 0
) vars,
People h
WHERE @r 0
) as InitialQ
ORDER BY Level Desc
精彩评论