CREATE TABLE sectors
(
sector_id integer PRIMARY KEY,
sector_name varchar(100) NOT NULL,
parent_sector_id integer REFERENCES sectors(sector_id)
);
INSERT INTO sectors(sector_id, sector_name, parent_sector_id)
SELECT 1, 'All sectors', NULL UNION ALL
SELECT 2, 'Business', 1 UNION ALL
SELECT 3, 'Manufacturing', 2 UNION ALL
SELECT 4, 'Retail', 2 UNION ALL
SELECT 5, 'Trading', 1 UNION ALL
SELECT 6, 'Nonprofit', 1 UNION ALL
SELECT 7, 'Agriculture', 1;
問題1:查找通過功能父關係PostgreSQL的層次關係功能
SELECT is_parent(1/*All sectors*/, 4/*Retail*/) should be true
SELECT is_parent(2/*Business*/, 4/*Retail*/) should be true
問題2:查找通過功能子女關係
SELECT is_child(4/*Retail*/, 1/*All sectors*/) should be true
SELECT is_child(4/*Retail*/, 2/*Business*/) should be true
任何幫助將是高度讚賞。
+1有關ltree數據類型的信息。可悲的是,我不能在這裏使用它。謝謝denis。 :) –