【发布时间】:2009-08-05 12:57:06
【问题描述】:
我正在努力解决排序问题。
我有一张如下表:
aspect_id (int)
aspect_text (memo)
root_id (int) which has as a foreign key a aspect_id
我有一个包含以下虚拟数据的非循环树:
aspect_id aspect_text root_id
1 root null
2 aspect1 1
3 aspect2 1
4 aspect3 2
5 aspect5 4
在示例中,数据已正确排序,但在我的数据库中却没有。我想排序它从根元素开始,然后找到一个孩子,输出那个孩子并递归地做。
使用 CTE 是相当可行的。 Access 不支持此功能。使用 CTE 会是这样的:
WITH aspectTree (aspect_id, root_id, Level#) AS
(
Select
aspect.aspect_id,
aspect.root_id,
0
FROM aspect
WHERE aspect.aspect_id = 44
UNION ALL
SELECT
aspect.aspect_id,
aspect.root_id,
T.Level# + 1
FROM aspect
INNER JOIN aspectTree AS T
On T.aspect_id = aspect.root_id
)
SELECT * FROM aspectTree;
【问题讨论】:
-
首字母缩略词 CTE 是什么意思?
-
CTE = 公用表表达式,是 SQL-99 标准的一部分,并被引入 SQL Server 2005。请参阅 MSDN“使用公用表表达式”(msdn.microsoft.com/en-us/library/ms190766.aspx)。
-
您使用的是哪个版本的 Access? Access 2010 有一个新的字段类型来处理分层数据。
标签: ms-access sorting treeview tree parent-child