我有一张桌子:
ID ParentID
3 1
7 2
4 3
5 4
如何使用sql脚本(递归方式)列出结果如下:
ID ParentID
3 1
4 3
5 4
7 2
你有什么想法吗?
答案 0 :(得分:3)
SELECT * FROM YourTable ORDER BY ID
会为您提供所需的订单。但如果你想要递归的那个:
SELECT childs.Id AS 'Child Id', Parents.Id As 'Parent'
FROM YourTable childs
INNER JOIN YourTable parents ON childs.ParentId = parents.Id
ORDER BY Parents.Id
答案 1 :(得分:1)
Select *
from MyTable
order By ID;
答案 2 :(得分:0)
Select * from tablename order by id