Get Left side from binary tree from mysql

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

My Table structure is,

username        leg1        leg2
------------------------------------
user1           user2       user7
user2           user3       user8
user3           user4       user9
user4           user5       test
user5           user6       user10

<img src="https://i.stack.imgur.com/YafXu.png" alt="enter image description here">

I need to get left tree of user1 (need to get tree based on leg1)

Username
--------

user2
user3
user4
user5

My updated question

Its working in minimum record. but if i used this SP in max no.of record(I have 4200 Records). It shows the error as "#1456 - Recursive limit 255 (as set by the max_sp_recursion_depth variable) was exceeded for routine getFinal "

DELIMITER $$
CREATE PROCEDURE `getFinal`(IN `param_name` VARCHAR(255), OUT `result` VARCHAR(255))
BEGIN
  DECLARE `next_param_name` VARCHAR(255);
   SELECT `leg1` INTO `next_param_name`  FROM `matrix`  WHERE `username` = `param_name`;
    IF `next_param_name` IS NULL THEN
    SET `result` := `param_name`;
  ELSE     
    CALL `getFinal`(`next_param_name`, `result`);
 END IF;
END$$
DELIMITER ;

Answers

MySQL does not support recursive calls, So you can not do that with traditional SQL queries. You can build a function in your desired programming language that you use, Or I think you can use Stored procedure.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/33730935/get-left-side-from-binary-tree-from-mysql

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils