add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table comments_table (
id int,
reply_to_id int,
content varchar(20)
);

insert into comments_table values
(12, NULL, 'TEXT'),
(13, 12, 'TEXT'),
(14, 12, 'TEXT'),
(15, 13, 'TEXT');
Records: 4  Duplicates: 0  Warnings: 0
WITH recursive cte (id, reply_to_id, path)
AS
(
SELECT id, reply_to_id, CAST(id AS CHAR(200)) AS path
FROM comments_table
WHERE reply_to_id = 12
UNION ALL
SELECT e.id, e.reply_to_id, CONCAT(cte.path, ",", e.id)
FROM comments_table AS e
JOIN cte ON e.reply_to_id = cte.id
)
SELECT *
from cte
order by path
id reply_to_id path
13 12 13
15 13 13,15
14 12 14