Home > database > How according to tree structure of the table, a node, found to his supervisor.
How according to tree structure of the table, a node, found to his supervisor.
Time:10-10
The create table [ceshi] (id int, parentid int, code nvarchar (50))
Insert into [ceshi] Select 1, 0, 'a' union all Select 2, 1, 'b' union all Select 3, 1, 'c' union all Select 4, 2, 'd' union all Select 5, 2, 'e' union all Select 6, 2, 'f' union all The select 7, 3, 'g' union all The select 8, 3, 'h' union all The select 9, 4, 'I' union all The select 10, 5, 'j' union all Select 11, 9, 'k' union all The select 12, 7, 'l' union all The select 13, 7, 'm' union all The select 14, 8, 'n' union all The select 15, 13, 'o' union all Select 16, 14, and 'p'
This is about data P_id is superior node I have G id should be found out. A, C, G, L, M, O C id should find out A, C, G, L, M, O, H, N, P.
How should this query, Use WITH the AS ?There are tens of thousands of data, Will slow, Consult,
CodePudding user response:
The create table [ceshi] (id int, parentid int, code nvarchar (50), longcode (200)) Insert into [ceshi] Select 1, 0, 'a', '1' union all Select 2, 1, 'b', '1.2' union all Select 3, 1, the 'c', '1.3' union all Select 4, 2, 'd', '1' union all Select 5, 2, 'e', '1.2.5' union all Select 6, 2, 'f', '1.2.6' union all The select 7, 3, 'g', '1.3.7' union all The select 8, 3, 'h', '1.3.8' union all The select 9, 4, 'I', '1.2.4.9' union all The select 10, 5, 'j', '1.2.5.10' union all Select 11, 9, 'k' and '1.2.4.9.11' union all The select 12, 7, 'l', '1.3.7.12' union all The select 13, 7, 'm', '1.3.7.13' union all The select 14, 8, 'n', '1.3.8.14' union all The select 15, 13, 'o', '1.3.7.13.15' union all The select 16, 14, the 'p', '1.3.8.14.15'
Add a column length of code, to save his supervisor ID, this check is more convenient,
CodePudding user response:
Tens of thousands of data CET statement can't be slow, plus a list of paths to check the lower is convenient, superior to use more, as are dealt with CET