不知道您使用的是什么数据库:如果是 SQL Server,请使用公用表表达式 ( CTE )
除此以外,
您需要某种代码或存储过程.. 使用 psuedoCode
Assuming @Parent is Primary key of Area record you want children of...
--Create Temp table (Does your DB have temp Tables) of Keys
-- Say it's called 'Children'
-- -- make this a temmp table...
-- In SQL Server syntax uses a #.
-- Create Table #Table... ( or use table variable Declare @Children Table ... ),
-- Oracle, MySql have their own syntax...
Create Table Children
(PK Integer Primary Key Not Null)
-- -------------------------
Insert Children(PK)
Select PK From Area
Where Parent = @Parent
-- -----------------------
While Exists (Select * From 'Children' As C
Where Exists
(Select * From Area
Where parent = C.PK
And PK Not In
(Select PK From 'Children')))
Begin
Insert Children(PK)
Select PK From Area
Where Parent In (Select PK From Children)
And PK Not In (Select PK From Children)
End
--Then join temp table to Area table and return results
Select a.* From Area a
Join Children C On C.PK = A.PK