i want to use recursive code in mysql but the server doens't support it
so plz give me some advice
this is my recursive query
mysql(Recursive)
WITH RECURSIVE CTE AS
(SELECT ssn, superssn, 0 as depth from
EMPLOYEE where ssn='888665555'
UNION ALL
SELECT A.SSN, A.SUPERSSN, depth + 1 FROM
EMPLOYEE A INNER JOIN CTE WHERE A.SUPERSSN
= CTE.SSN )
SELECT * FROM CTE;
i try this
mysql> select ssn,superssn
from(select * from EMPLOYEE order by superssn,ssn) EMPLOYEE_sorted,
(select @pv := '888665555') initialisation where find_in_set(superssn, @pv)
and length(@pv := concat(@pv,',',ssn));
the result
+-----------+-----------+
| ssn | superssn |
+-----------+-----------+
| 333445555 | 888665555 |
| 987654321 | 888665555 |
| 987987987 | 987654321 |
| 999887777 | 987654321 |
| 000000001 | 999887777 |
+-----------+-----------+
but the correct result is
+-----------+-----------+
| ssn | superssn |
+-----------+-----------+
| 000000001 | 999887777 |
| 000000002 | 000000001 |
| 000000003 | 000000002 |
| 000000004 | 000000003 |
| 000000005 | 000000004 |
| 000000006 | 000000005 |
| 123456789 | 333445555 |
| 333445555 | 888665555 |
| 453453453 | 333445555 |
| 666884444 | 333445555 |
| 987654321 | 888665555 |
| 987987987 | 987654321 |
| 999887777 | 987654321 |
+-----------+-----------+
data table select ssn, superssn from EMPLOYEE;
+-----------+-----------+
| ssn | superssn |
+-----------+-----------+
| 888665555 | NULL |
| 000000002 | 000000001 |
| 000000003 | 000000002 |
| 000000004 | 000000003 |
| 000000005 | 000000004 |
| 000000006 | 000000005 |
| 123456789 | 333445555 |
| 453453453 | 333445555 |
| 666884444 | 333445555 |
| 333445555 | 888665555 |
| 987654321 | 888665555 |
| 987987987 | 987654321 |
| 999887777 | 987654321 |
| 000000001 | 999887777 |
+-----------+-----------+
this is data table you can use reference could someone give advice how can i change it?! thanks
Comments
Post a Comment