Home > database >  MYSQL recursive query great god help solve problems
MYSQL recursive query great god help solve problems

Time:09-20


Set the admin to top, pname use_name is introduced in the same line, how do the opening of the admin admin statistics how many layer is introduced, pname use_name once, for a layer is introduced, the MYSQL query how to write

CodePudding user response:

With RECURSIVE TMP (user_name, p_name) as (
Select the 'admin' as user_name, null as p_name from dual union all
Select 'gs1 as user_name,' admin 'as p_name from dual union all
Select 'gs2 as user_name,' gs1 as p_name from dual union all
Select 'gs3 as user_name,' gs1 as p_name from dual union all
Select 'gs4 as user_name,' gs2 as p_name from dual union all
Select 'CQC as user_name,' gs4 as p_name from dual union all
Select 'BXP as user_name,' CQC as p_name from dual),
Tmp1 (user_name, p_name, LVL, p_path) as (
The select user_name, p_name, 0 as LVL, cast (user_name as char (200)) as p_path from TMP where p_name is null
Union all
The select t2 user_name, t2 p_name, t1. + 1 as LVL LVL, concat (t1) p_path, '- & gt; ', t2. User_name) as the path
The from tmp1 t1 inner join TMP t2 on t1. User_name=t2. P_name)
Select * from tmp1;

CodePudding user response:

https://dev.mysql.com/doc/refman/8.0/en/with.html read manuals, basically have inside,
  • Related