在我的数据库中,我有下表:
Category: {[Name: VarChar, TopCategory: VarChar]}该表包含以下内容:

现在,我需要在递归语句中使用with-clause从类别Computer Science的所有子类别中获取所有名称。这必须使用SQL完成,没有PHP或其他编程语言。
所有子类别不仅指直接后代,还指本例中的C++和Java,我该怎么做呢?
到目前为止我所做的是:
SELECT name FROM category WHERE (topcategory = 'Computer Science')发布于 2012-09-28 15:31:04
我们开始吧:
WITH RECURSIVE cte_t1 (name, topcategory, Level)
AS
(
SELECT name, topcategory, 0 AS Level
FROM Category
WHERE topcategory = N'ComputerScience'
UNION ALL
SELECT t1.name, t1.topcategory, Level + 1
FROM Category t1, cte_t1 ctet1
WHERE ctet1.name= t1.topcategory
)
SELECT Level, topcategory, name
FROM cte_t1发布于 2012-09-28 15:37:48
如果您的数据库是oracle,您可以尝试一下(sql fiddle):
select * from category
start with name = 'Computer Science'
connect by prior name = top_categoryhttps://stackoverflow.com/questions/12635405
复制相似问题