I have the following table structure:
CREATE TABLE "Material" (
"MaterialId" TEXT NOT NULL CONSTRAINT "PK_Material" PRIMARY KEY,
"Content" TEXT NULL,
"Title" TEXT NULL,
"Paid" INTEGER NOT NULL,
"Published" INTEGER NOT NULL,
"ParentMaterialId" TEXT NULL,
"Dates" TEXT NULL,
"Private" INTEGER NOT NULL,
CONSTRAINT "FK_Material_Material_ParentMaterialId" FOREIGN KEY ("ParentMaterialId") REFERENCES "Material" ("MaterialId") ON DELETE RESTRICT
)
Each material can have an infinite sub materials inside them, and i would like:
- Get the outer most ancestor material for a particular child.
- Get the First ancestor that is
Paid
(Paid is a boolean)
I am not that good at complex database operations, so please be kind.
Best Answer
Both SQLite and SQL Server understand enough standard SQL to do a recursive tree walk like this: