Mysql
 sql >> Database >  >> RDS >> Mysql

Tentativo di restituire tutte le colonne di record figlio per un record padre

Potresti usare CSV gli ID risultano come:

SELECT *
FROM tblreport
WHERE FIND_IN_SET(ID,(SELECT GROUP_CONCAT(lv SEPARATOR ',') FROM (
                     SELECT @pv:=(SELECT GROUP_CONCAT(ID SEPARATOR ',') 
                     FROM tblreport WHERE ParentID IN (@pv)) AS lv FROM tblreport
                     JOIN (SELECT @pv:=1)tmp
                       WHERE ParentID IN (@pv)) a));

Demo DBFiddle

Uscita:

ID  Name    ParentID
2   2nd     1
3   3rd     1
4   4th     2

MODIFICA:

Se hai bisogno anche della riga originale puoi usare UNION ALL :

SET @var = 1;

SELECT *
FROM tblreport
WHERE FIND_IN_SET(ID,(SELECT GROUP_CONCAT(lv SEPARATOR ',') FROM (
                     SELECT @pv:=(SELECT GROUP_CONCAT(ID SEPARATOR ',') 
                     FROM tblreport WHERE ParentID IN (@pv)) AS lv FROM tblreport
                     JOIN (SELECT @pv:[email protected])tmp
                       WHERE ParentID IN (@pv)) a))

UNION ALL
SELECT *
FROM tblReport
WHERE ID = @var
ORDER BY ID;

DBFiddle Demo2