# sqlite3
CREATE TABLE IF NOT EXISTS "comments"
(
"comment_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"comment_parent" INTEGER DEFAULT 0,
"comment_text" TEXT
);
INSERT INTO comments (comment_id, comment_parent, comment_text) VALUES ("1", "0", "first message");
INSERT INTO comments (comment_id, comment_parent, comment_text) VALUES ("2", "1", "first reply to first");
INSERT INTO comments (comment_id, comment_parent, comment_text) VALUES ("3", "1", "second reply to first");
INSERT INTO comments (comment_id, comment_parent, comment_text) VALUES ("4", "2", "first reply to second");
INSERT INTO comments (comment_id, comment_parent, comment_text) VALUES ("5", "2", "second reply to second");
WITH clause AS
(
SELECT *
FROM comments
WHERE comment_parent = 0
UNION ALL
SELECT child.*
FROM comments AS child
JOIN clause AS parent ON child.comment_parent = parent.comment_id
)
SELECT *
FROM clause;
оно выводит в порядке «как есть», а надо чтобы выводил
1|0|first message
2|1|first reply to first
3|1|second reply to first
4|2|first reply to second
5|2|second reply to second
щито я делаю не так?
получается выводит так, что 4 и 5 являются ответами на 3, хотя у них родитель 2.