开发者

I want to pass in 1 inplace of 4 and that result set should return in reverse order

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        3

and 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,

Kishor


SELECT _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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜