Query to get id's

0

I have this query,

I have this table with the columns id, name, path.

What I need is for the path chain to take the value of the id and replace them with the name, thus remaining

| ID | name                       | path  |   path2    
|----|----------------------------|-------|------------
|  1 | Training                   | /1    | /Training           
|  2 | 1438 - Hutsville, AL, USA  | /1/4/2| /Training/Plants/1438 - Hutsville, AL, USA
|  4 | Plant                      | /1/4  | /Training/Plants
|  5 | Corporate Quality          | /1/5  | /Training/Corporate Quality
|  6 | Orientation                | /1/5/6| /Training/Corporate Quality/Orientation

Any suggestions I can use?

Greetings

    
asked by Edgar Conrado 21.07.2017 в 18:42
source

3 answers

1

You have to use a function to do all this process for you, like this:

VIEW DEMO

NOTE: The function can be improved, I do not use mysql, that's why it's like that.

Greetings.

    
answered by 22.07.2017 / 00:07
source
0

I could solve it by doing a Store Procedured, applying a reverse group_concat, I leave it in case someone has the same problem as me.

CREATE DEFINER='moodle'@'%' PROCEDURE 'getCategogies'()
BEGIN

DECLARE description VARCHAR(255);
DECLARE id_k INT DEFAULT 0;
DECLARE value TEXT;
DECLARE occurance INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE splitted_value INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT id, path
                                     FROM moodle.mdl_course_categories;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE table2(
'id' INT NOT NULL,
'value' VARCHAR(255) NOT NULL,
'value2' VARCHAR(255)

) ENGINE=Memory;

OPEN cur1;
  read_loop: LOOP
    FETCH cur1 INTO id_k, value;
    IF done THEN
      LEAVE read_loop;
    END IF;

    SET occurance = (SELECT LENGTH(value)
                             - LENGTH(REPLACE(value, '/', ''))
                             +1);

    SET description = '';
    SET i=2;
    WHILE i <= occurance DO
      SET splitted_value =
      REPLACE((SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, '/', i),
      LENGTH(SUBSTRING_INDEX(value, '/', i - 1)) + 1), ',', '')),'/','');

     SET description =  CONCAT(description,'/',(SELECT name FROM moodle.mdl_course_categories WHERE id = splitted_value));

      SET i = i + 1;

    END WHILE;


    INSERT INTO table2 VALUES (id_k, value,description);

  END LOOP;

  SELECT * FROM table2;

 CLOSE cur1;
 END
    
answered by 22.07.2017 в 00:31
0

Also using GROUP_CONCAT () but with an additional table with the path.

I share my test

SELECT  np.id as id, 
        GROUP_CONCAT(np.idpad  order by np.idpad SEPARATOR "/") as pathpad,
        GROUP_CONCAT(n.name    order by np.idpad SEPARATOR "/") as pathunion
  FROM  tb_name n inner join tb_namepath np
    on  n.id = np.idpad 
  group by np.id 
  order by np.id
    
answered by 22.07.2017 в 01:59