-3

我有一张桌子:

ID  ParentID
3   1
7   2
4   3
5   4

如何使用 sql 脚本(递归方式)列出这样的结果:

ID  ParentID
3   1
4   3
5   4
7   2

你有想法吗?

4

3 回答 3

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
于 2012-09-05T08:58:11.217 回答
1
Select * 
from MyTable 
order By ID;
于 2012-09-05T08:53:36.310 回答
0
Select * from tablename order by id
于 2012-09-05T08:55:58.247 回答