> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/ltree/ltree-ops-funcs.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.22.2. 運算子與函式

## F.22.2. 運算子與函式 [#](#ltree-ops-funcs) <a href="#ltree-ops-funcs" id="ltree-ops-funcs"></a>

型別 `ltree` 具有一般比較運算子 `=`、`<>`、`<`、`>`、`<=`、`>=`。比較會依樹狀走訪順序排序，節點子項目則依標籤文字排序。此外，還提供[表 F.12](#ltree-op-table) 所示的專用運算子。

### 表 F.12. `ltree` 運算子 [#](#ltree-op-table) <a href="#ltree-op-table" id="ltree-op-table"></a>

| <p>運算子</p><p>說明</p>                                                                                                                                                                                                                               |   |                                                                                     |   |                                                                                          |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - | ----------------------------------------------------------------------------------- | - | ---------------------------------------------------------------------------------------- |
| <p><code>ltree</code> <code>@></code> <code>ltree</code> → <code>boolean</code></p><p>左側引數是否為右側引數的祖先（或相等）？</p>                                                                                                                                    |   |                                                                                     |   |                                                                                          |
| <p><code>ltree</code> <code><@</code> <code>ltree</code> → <code>boolean</code></p><p>左側引數是否為右側引數的子孫（或相等）？</p>                                                                                                                                    |   |                                                                                     |   |                                                                                          |
| <p><code>ltree</code> <code>~~</code> <code>lquery</code> → <code>boolean</code></p><p><code>lquery</code> <code>~~</code> <code>ltree</code> → <code>boolean</code></p><p><code>ltree</code> 是否符合 <code>lquery</code>？</p>                       |   |                                                                                     |   |                                                                                          |
| <p><code>ltree</code> <code>?</code> <code>lquery\[]</code> → <code>boolean</code></p><p><code>lquery\[]</code> <code>?</code> <code>ltree</code> → <code>boolean</code></p><p><code>ltree</code> 是否符合陣列中的任何 <code>lquery</code>？</p>             |   |                                                                                     |   |                                                                                          |
| <p><code>ltree</code> <code>@</code> <code>ltxtquery</code> → <code>boolean</code></p><p><code>ltxtquery</code> <code>@</code> <code>ltree</code> → <code>boolean</code></p><p><code>ltree</code> 是否符合 <code>ltxtquery</code>？</p>                |   |                                                                                     |   |                                                                                          |
| <p><code>ltree</code> <code>                                                                                                                                                                                                                      |   | </code> <code>ltree</code> → <code>ltree</code></p><p>串接 <code>ltree</code> 路徑。</p> |   |                                                                                          |
| <p><code>ltree</code> <code>                                                                                                                                                                                                                      |   | </code> <code>text</code> → <code>ltree</code></p><p><code>text</code> <code>       |   | </code> <code>ltree</code> → <code>ltree</code></p><p>將文字轉換為 <code>ltree</code> 並串接。</p> |
| <p><code>ltree\[]</code> <code>@></code> <code>ltree</code> → <code>boolean</code></p><p><code>ltree</code> <code><@</code> <code>ltree\[]</code> → <code>boolean</code></p><p>陣列是否包含 <code>ltree</code> 的祖先？</p>                                 |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code><@</code> <code>ltree</code> → <code>boolean</code></p><p><code>ltree</code> <code>@></code> <code>ltree\[]</code> → <code>boolean</code></p><p>陣列是否包含 <code>ltree</code> 的子孫？</p>                                 |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>~~</code> <code>lquery</code> → <code>boolean</code></p><p><code>lquery</code> <code>~~</code> <code>ltree\[]</code> → <code>boolean</code></p><p>陣列是否包含符合 <code>lquery</code> 的任何路徑？</p>                          |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>?</code> <code>lquery\[]</code> → <code>boolean</code></p><p><code>lquery\[]</code> <code>?</code> <code>ltree\[]</code> → <code>boolean</code></p><p><code>ltree</code> 陣列是否包含符合任何 <code>lquery</code> 的任何路徑？</p> |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>@</code> <code>ltxtquery</code> → <code>boolean</code></p><p><code>ltxtquery</code> <code>@</code> <code>ltree\[]</code> → <code>boolean</code></p><p>陣列是否包含符合 <code>ltxtquery</code> 的任何路徑？</p>                   |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>?@></code> <code>ltree</code> → <code>ltree</code></p><p>傳回第一個為 <code>ltree</code> 祖先的陣列項目；若沒有則傳回 <code>NULL</code>。</p>                                                                                           |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>?<@</code> <code>ltree</code> → <code>ltree</code></p><p>傳回第一個為 <code>ltree</code> 子孫的陣列項目；若沒有則傳回 <code>NULL</code>。</p>                                                                                           |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>?\~</code> <code>lquery</code> → <code>ltree</code></p><p>傳回第一個符合 <code>lquery</code> 的陣列項目；若沒有則傳回 <code>NULL</code>。</p>                                                                                          |   |                                                                                     |   |                                                                                          |
| <p><code>ltree\[]</code> <code>?@</code> <code>ltxtquery</code> → <code>ltree</code></p><p>傳回第一個符合 <code>ltxtquery</code> 的陣列項目；若沒有則傳回 <code>NULL</code>。</p>                                                                                     |   |                                                                                     |   |                                                                                          |

<br>

`<@`、`@>`、`@` 與 `~` 運算子有對應的 `^<@`、`^@>`、`^@`、`^~`；它們相同，但不使用索引。這些只適合測試用途。

可用函式列於[表 F.13](#ltree-func-table)。

### 表 F.13. `ltree` 函式 [#](#ltree-func-table) <a href="#ltree-func-table" id="ltree-func-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>subltree</code> ( <code>ltree</code>, <em><code>start</code></em> <code>integer</code>, <em><code>end</code></em> <code>integer</code> ) → <code>ltree</code></p><p>傳回 <code>ltree</code> 從位置 <em><code>start</code></em> 到位置 <em><code>end</code></em>-1 的子路徑（從 0 起算）。</p><p><code>subltree('Top.Child1.Child2', 1, 2)</code> → <code>Child1</code></p>                                                                                                                        |
| <p><code>subpath</code> ( <code>ltree</code>, <em><code>offset</code></em> <code>integer</code>, <em><code>len</code></em> <code>integer</code> ) → <code>ltree</code></p><p>傳回從位置 <em><code>offset</code></em> 開始、長度為 <em><code>len</code></em> 的 <code>ltree</code> 子路徑。若 <em><code>offset</code></em> 為負數，子路徑從距路徑結尾該數量的位置開始。若 <em><code>len</code></em> 為負數，會從路徑結尾移除該數量的標籤。</p><p><code>subpath('Top.Child1.Child2', 0, 2)</code> → <code>Top.Child1</code></p>                       |
| <p><code>subpath</code> ( <code>ltree</code>, <em><code>offset</code></em> <code>integer</code> ) → <code>ltree</code></p><p>傳回從位置 <em><code>offset</code></em> 開始並延伸至路徑結尾的 <code>ltree</code> 子路徑。若 <em><code>offset</code></em> 為負數，子路徑從距路徑結尾該數量的位置開始。</p><p><code>subpath('Top.Child1.Child2', 1)</code> → <code>Child1.Child2</code></p>                                                                                                                                             |
| <p><code>nlevel</code> ( <code>ltree</code> ) → <code>integer</code></p><p>傳回路徑中的標籤數量。</p><p><code>nlevel('Top.Child1.Child2')</code> → <code>3</code></p>                                                                                                                                                                                                                                                                                                                               |
| <p><code>index</code> ( <em><code>a</code></em> <code>ltree</code>, <em><code>b</code></em> <code>ltree</code> ) → <code>integer</code></p><p>傳回 <em><code>b</code></em> 在 <em><code>a</code></em> 中第一次出現的位置；若找不到則傳回 -1。</p><p><code>index('0.1.2.3.5.4.5.6.8.5.6.8', '5.6')</code> → <code>6</code></p>                                                                                                                                                                                 |
| <p><code>index</code> ( <em><code>a</code></em> <code>ltree</code>, <em><code>b</code></em> <code>ltree</code>, <em><code>offset</code></em> <code>integer</code> ) → <code>integer</code></p><p>傳回 <em><code>b</code></em> 在 <em><code>a</code></em> 中第一次出現的位置；若找不到則傳回 -1。搜尋從位置 <em><code>offset</code></em> 開始；若 <em><code>offset</code></em> 為負數，則從距離路徑結尾 <em><code>-offset</code></em> 個標籤的位置開始。</p><p><code>index('0.1.2.3.5.4.5.6.8.5.6.8', '5.6', -4)</code> → <code>9</code></p> |
| <p><code>text2ltree</code> ( <code>text</code> ) → <code>ltree</code></p><p>將 <code>text</code> 轉型為 <code>ltree</code>。</p>                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>ltree2text</code> ( <code>ltree</code> ) → <code>text</code></p><p>將 <code>ltree</code> 轉型為 <code>text</code>。</p>                                                                                                                                                                                                                                                                                                                                                              |
| <p><code>lca</code> ( <code>ltree</code> \[, <code>ltree</code> \[, ... ]] ) → <code>ltree</code></p><p>計算各路徑最長的共同祖先路徑（最多支援 8 個引數）。</p><p><code>lca('1.2.3', '1.2.3.4.5.6')</code> → <code>1.2</code></p>                                                                                                                                                                                                                                                                                |
| <p><code>lca</code> ( <code>ltree\[]</code> ) → <code>ltree</code></p><p>計算陣列中各路徑最長的共同祖先路徑。</p><p><code>lca(array\['1.2.3'::ltree,'1.2.3.4'])</code> → <code>1.2</code></p>                                                                                                                                                                                                                                                                                                              |

<br>

[返回 F.22. ltree — 階層式樹狀資料型別](https://docs.postgresql.tw/19/appendixes/contrib/ltree.md#ltree)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ltree.html)（核對日期：2026-09-11）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/ltree/ltree-ops-funcs.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
