> 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/the-sql-language/sql-syntax/sql-syntax-lexical/sql-precedence.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 4.1.6. 運算子優先順序

## 4.1.6. 運算子優先順序 [#](#sql-precedence) <a href="#sql-precedence" id="sql-precedence"></a>

[表 4.2](#sql-precedence-table) 列出了 PostgreSQL 中運算子的優先順序與結合性。大多數運算子具有相同的優先順序，並且是左結合的。運算子的優先順序與結合性是寫死在剖析器中的。如果你希望包含多個運算子的運算式以不同於優先順序規則所暗示的方式剖析，請加上括號。

### 表 4.2. 運算子優先順序（由高至低） [#](#sql-precedence-table) <a href="#sql-precedence-table" id="sql-precedence-table"></a>

| 運算子／元素                                  | 結合性 | 說明                                                   |
| --------------------------------------- | --- | ---------------------------------------------------- |
| `.`                                     | 左   | 資料表／欄位名稱分隔符號                                         |
| `::`                                    | 左   | PostgreSQL 風格的型別轉換                                   |
| `[` `]`                                 | 左   | 陣列元素選取                                               |
| `+` `-`                                 | 右   | 一元正號、一元負號                                            |
| `COLLATE`                               | 左   | 定序選取                                                 |
| `AT`                                    | 左   | `AT TIME ZONE`、`AT LOCAL`                            |
| `^`                                     | 左   | 乘冪                                                   |
| `*` `/` `%`                             | 左   | 乘法、除法、取餘數                                            |
| `+` `-`                                 | 左   | 加法、減法                                                |
| （任何其他運算子）                               | 左   | 所有其他原生與使用者自訂的運算子                                     |
| `BETWEEN` `IN` `LIKE` `ILIKE` `SIMILAR` |     | 範圍包含、集合成員資格、字串比對                                     |
| `<` `>` `=` `<=` `>=` `<>`              |     | 比較運算子                                                |
| `IS` `ISNULL` `NOTNULL`                 |     | `IS TRUE`、`IS FALSE`、`IS NULL`、`IS DISTINCT FROM` 等等 |
| `NOT`                                   | 右   | 邏輯否定                                                 |
| `AND`                                   | 左   | 邏輯且                                                  |
| `OR`                                    | 左   | 邏輯或                                                  |

<br>

請注意，運算子優先順序規則也適用於與上述內建運算子同名的使用者自訂運算子。例如，如果你為某個自訂資料型別定義了一個「+」運算子，不論你的運算子做什麼事，它都會與內建的「+」運算子具有相同的優先順序。

在 `OPERATOR` 語法中使用以 schema 限定的運算子名稱時，例如：

```

SELECT 3 OPERATOR(pg_catalog.+) 4;
```

`OPERATOR` 結構會被視為具有[表 4.2](#sql-precedence-table) 中「任何其他運算子」的預設優先順序。無論 `OPERATOR()` 中出現的是哪一個特定運算子，都是如此。

### 注意

9.5 版之前的 PostgreSQL 使用略為不同的運算子優先順序規則。特別是，`<=`、`>=` 與 `<>` 過去被當作一般的運算子處理；`IS` 測試過去具有較高的優先順序；而 `NOT BETWEEN` 及相關結構的行為則不一致，在某些情況下會被當作具有 `NOT` 的優先順序，而不是 `BETWEEN` 的優先順序。這些規則之所以變更，是為了更符合 SQL 標準，並減少對邏輯上等價之結構處理不一致所造成的混淆。在大多數情況下，這些變更不會造成任何行為上的改變，或者可能會造成「no such operator」（找不到這種運算子）的錯誤，而這可以透過加上括號來解決。不過，也有一些特殊情況下，查詢的行為可能會改變，卻不會回報任何剖析錯誤。

[返回 4.1. 詞彙結構](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical.md#sql-syntax-lexical)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/sql-syntax-lexical.html)（原文版本：18.6；核對日期：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/the-sql-language/sql-syntax/sql-syntax-lexical/sql-precedence.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.
