我有一个表:
ID ParentID
3 1
7 2
4 3
5 4
我如何使用sql脚本(递归方式)列出这样的结果:
ID ParentID
3 1
4 3
5 4
7 2
你知道吗?
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
Select *
from MyTable
order By ID;
Select * from tablename order by id