參考連接git
樹狀數據存儲與查詢(非遞歸) - Use ltree extension deal tree-like data type(德哥github博文)github
postgresql ltree類型(推酷)sql
ltree的官方介紹 函數
安裝 "ltree" 索引擴展(使用超級用戶)post
create extension ltree;
查看ui
select * from pg_extension ; \dT
數據類型介紹spa
ltree (目前只支持A-Z,a-z,0-9,_做爲label的合法字符)postgresql
樹形結構類型,一個ltree被稱爲一個path,由1或多個LABEL組成,每一個label由A-Za-z0-9_組成。ltree是由標籤和分隔符組成的字符串,好比:L1.L2.L3code
lqueryblog
規則表達式,用於匹配ltree類型.
具體參考手冊,須要注意的是%匹配的不是一個label,而是label裏的一個單詞(_爲單詞分隔符
ltxtquery
通常用於全文掃描,注意,只有ltxtquery類型是符號和匹配的內容是能夠有空格隔開的,lquery和ltree不支持空格。
操做符介紹:
Operator | Returns | Description |
---|---|---|
ltree @> ltree | boolean | is left argument an ancestor of right (or equal)? |
ltree <@ ltree | boolean | is left argument a descendant of right (or equal)? |
ltree ~ lquery | boolean | does ltree match lquery? |
lquery ~ ltree | boolean | does ltree match lquery? |
ltree ? lquery[] | boolean | does ltree match any lquery in array? |
lquery[] ? ltree | boolean | does ltree match any lquery in array? |
ltree @ ltxtquery | boolean | does ltree match ltxtquery? |
ltxtquery @ ltree | boolean | does ltree match ltxtquery? |
ltree || ltree | ltree | concatenate ltree paths |
ltree || text | ltree | convert text to ltree and concatenate |
text || ltree | ltree | convert text to ltree and concatenate |
ltree[] @> ltree | boolean | does array contain an ancestor of ltree? |
ltree <@ ltree[] | boolean | does array contain an ancestor of ltree? |
ltree[] <@ ltree | boolean | does array contain a descendant of ltree? |
ltree @> ltree[] | boolean | does array contain a descendant of ltree? |
ltree[] ~ lquery | boolean | does array contain any path matching lquery? |
lquery ~ ltree[] | boolean | does array contain any path matching lquery? |
ltree[] ? lquery[] | boolean | does ltree array contain any path matching any lquery? |
lquery[] ? ltree[] | boolean | does ltree array contain any path matching any lquery? |
ltree[] @ ltxtquery | boolean | does array contain any path matching ltxtquery? |
ltxtquery @ ltree[] | boolean | does array contain any path matching ltxtquery? |
ltree[] ?@> ltree | ltree | first array entry that is an ancestor of ltree; NULL if none |
ltree[] ?<@ ltree | ltree | first array entry that is a descendant of ltree; NULL if none |
ltree[] ?~ lquery | ltree | first array entry that matches lquery; NULL if none |
ltree[] ?@ ltxtquery | ltree | first array entry that matches ltxtquery; NULL if none |
函數簡單介紹:
Function | Return Type | Description | Example | Result |
---|---|---|---|---|
subltree(ltree, int start, int end) | ltree | subpath of ltree from position start to position end-1 (counting from 0) | subltree('Top.Child1.Child2',1,2) | Child1 |
subpath(ltree, int offset, int len) | ltree | subpath of ltree starting at position offset, length len. If offset is negative, subpath starts that far from the end of the path. If len is negative, leaves that many labels off the end of the path. | subpath('Top.Child1.Child2',0,2) | Top.Child1 |
subpath(ltree, int offset) | ltree | subpath of ltree starting at position offset, extending to end of path. If offset is negative, subpath starts that far from the end of the path. | subpath('Top.Child1.Child2',1) | Child1.Child2 |
nlevel(ltree) | integer | number of labels in path | nlevel('Top.Child1.Child2') | 3 |
index(ltree a, ltree b) | integer | position of first occurrence of b in a; -1 if not found | index('0.1.2.3.5.4.5.6.8.5.6.8','5.6') | 6 |
index(ltree a, ltree b, int offset) | integer | position of first occurrence of b in a, searching starting at offset; negative offsetmeans start -offset labels from the end of the path | index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-4) | 9 |
text2ltree(text) | ltree | cast text to ltree | - | - |
ltree2text(ltree) | text | cast ltree to text | - | - |
lca(ltree, ltree, ...) | ltree | lowest common ancestor, i.e., longest common prefix of paths (up to 8 arguments supported) | lca('1.2.2.3','1.2.3.4.5.6') | 1.2 |
lca(ltree[]) | ltree | lowest common ancestor, i.e., longest common prefix of paths | lca(array['1.2.2.3'::ltree,'1.2.3']) | 1.2 |
2.1 推酷、德哥博客都用到的
建表
create table public.test( id serial, song ltree not null);
初始化後的數據