1
I am trying to make a query to a Sqlite database, I have the following tables:
Table suggestions:
CREATE TABLE IF NOT EXISTS sugestoes (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
texto VARCHAR(250),
autor VARCHAR(250),
itens VARCHAR(250),
pontos INTEGER
);
Vertical table
CREATE TABLE IF NOT EXISTS vertices (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
id_node_pai INTEGER,
id_node_filho INTEGER,
FOREIGN KEY (id_node_pai) REFERENCES sugestoes(id),
FOREIGN KEY (id_node_filho) REFERENCES sugestoes(id)
);
Within them I have the following data:
I would like to get the following output:
I have made the following consultation:
WITH vertices AS (
SELECT p.autor FROM sugestoes p, vertices e
WHERE e.id_node_pai = p.id
UNION ALL
SELECT f.autor FROM sugestoes f
INNER JOIN vertices d
ON f.id = d.id_node_filho
)
SELECT * FROM vertices;
However I have obtained the following error:
Error: circular reference: vertices
I tried to use the WITH RECURSIVE
, but also unsuccessful.
Need to be a recursive query? You tried with a subquery in the SELECT clause, making a query in the table
sugestoes.id
across the countrysidevertices.id_node_filho
within the main consultation?– Gomiero
@Gomiero, I confess that I do not know what is a subquery, I thought about the recursion precisely because it makes call the same table, but being a parent and a child, I took a look at several people with similar problems, but none solved this mine, if I do only research by
UNION
, it returns me all the values I want, but in the form of a single table, what I need is precisely the parent-child relationship between them, I will do a test with subquery to see.– bruno101