New with Recursive CTE clause with SQLite

SQLite 3.8.3 adds support for CTE. I tried some sample CTEs on this page and they work fine. However, after reading the documentation and trying to adjust some examples, I can’t Create a simple test.

First, I create a simple table with two fields: id and parent. This will create a simple tree or linked list of records:

< p>

CREATE TABLE test(id INTEGER PRIMARY KEY ASC,parent INTEGER);

Now I fill it with a few lines:

< pre>INSERT INTO test (parent) VALUES (NULL);
INSERT INTO test (parent) VALUES (1);
INSERT INTO test (parent) VALUES (2);
INSERT INTO test (parent) VALUES (3);

After that I have a table that looks like this:

---+------ -
id | parent
---+-------
1 | NULL
2 | 1
3 | 2
4 | 3

Now I want to generate a list of rows along the path between 3 and 1:

WITH RECURSIVE test1(id,parent) AS (
VALUES(3,2)
UNION ALL
SELECT * FROM test WHERE test.parent=test1.id)
SELECT * FROM test1;

But I get error :

no such column: test1.id

test and test1 both have an id field, why does it claim that it does not exist? I have checked the documentation many times, but I have not seen my error. What am I doing wrong?

It is necessary to include the test1 table in the SELECT:

< /p>

WITH RECURSIVE test1(id,parent) AS (
VALUES(3,2)
UNION ALL
SELECT test.id,test.parent FROM test,test1 WHERE test1 .parent=test.id)
SELECT * FROM test1;

Please note that the WHERE clause has been reversed, and the original test in the question returns from the current line to the end, not from the end Back to the beginning.

SQLite 3.8.3 adds support for CTE. I tried some sample CTEs on this page and they work fine. However, after reading the documentation and After trying to adjust some examples, I cannot create a simple test.

First, I create a simple table with two fields: id and parent. This will create a simple tree or linked Record list:

CREATE TABLE test(id INTEGER PRIMARY KEY ASC,parent INTEGER);

Now I fill it with a few lines:

INSERT INTO test (parent) VALUES (NULL); 
INSERT INTO test (parent) VALUES (1);
INSERT INTO test (parent) VALUES (2) ;
INSERT INTO test (parent) VALUES (3);

After that I have a table that looks like this:

-- -+-------
id | parent
---+-------
1 | NULL
2 | 1
3 | 2
4 | 3

Now I want to generate a list of rows along the path between 3 and 1:

WITH RECURSIVE test1(id,parent) AS (
VALUES(3,2)
UNION ALL
SELECT * FROM test WHERE test.parent=test1.id)
SELECT * FROM test1;

But I get the error:

no such column: test1.id

test and test1 have one id field, why does it claim that it does not exist? I have checked the documentation many times, but I have not seen my error. What am I doing wrong?

It is necessary to include the test1 table in the SELECT:

WITH RECURSIVE test1(id, parent) AS (
VALUES(3,2)
UNION ALL
SELECT test.id,test.parent FROM test,test1 WHERE test1.parent=test.id)
SELECT * FROM test1;

Please note that the WHERE clause has been reversed, and the original test in the question returns from the current line to the end, not from the end to the beginning.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 915 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.