Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ FIELD(<expr>, <param> [, ...])

## 举例

```sql
-- setup
CREATE TABLE baseall (k1 INT, k7 VARCHAR(64))
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO baseall VALUES
(1, 'wangjing04'),
(2, 'wangyu14'),
(3, 'yuanyuan06');

CREATE TABLE class_test (class_name VARCHAR(64))
DISTRIBUTED BY HASH(class_name) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO class_test VALUES
('Suzi'), ('Suzi'),
('Ben'), ('Ben'),
('Henry'), ('Henry'),
(NULL);
```

直接使用 `FIELD` 查找位置(基于 1 的索引):

```sql
Expand Down Expand Up @@ -65,6 +87,7 @@ SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Hen
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
Expand All @@ -88,6 +111,7 @@ SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Hen
| Ben |
| Suzi |
| Suzi |
| NULL |
+------------+
```

Expand All @@ -107,4 +131,4 @@ SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Hen
| Henry |
| Henry |
+------------+
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check notice on line 1 in i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-section-order

SQL function docs must contain sections in this order%3A Description%2C Syntax%2C Parameters%2C Return Value%2C Example. Missing%3A Example. Owner%3A @zclllyybb

Check warning on line 1 in i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

seo-title-duplicate

Rendered SEO title is duplicated across indexable pages%3A "FIELD - Apache Doris". Add a version%2C locale%2C or page-specific qualifier. Owner%3A @zclllyybb
{
"title": "FIELD",
"language": "zh-CN",
Expand All @@ -9,14 +9,14 @@
## 描述

返回 `<expr>` 在参数列表中的位置(基于 1 的索引),常用于 `ORDER BY` 子句中实现自定义排序。如果 `<expr>` 不在参数列表中,或 `<expr>` 为 `NULL`,则返回 `0`。在自定义排序中,不在参数列表中的数据会被排到最前面,可通过 `asc` 或 `desc` 控制整体排序顺序;对于 `NULL` 值,可以使用 `nulls first` 或 `nulls last` 控制排序顺序。

Check notice on line 12 in i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-parameters-table

Parameters section must be a Markdown table with Parameter and Description columns. Owner%3A @zclllyybb
## 语法

```sql
FIELD(<expr>, <param> [, ...])
```

## 参数

Check notice on line 19 in i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-return-type

Return Value section must state the returned type. Owner%3A @zclllyybb

| 参数 | 说明 |
|------------|-----------------------|
Expand All @@ -29,6 +29,42 @@

## 举例

```sql
-- setup
CREATE TABLE baseall (k1 INT, k7 VARCHAR(64))
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO baseall VALUES
(1, 'wangjing04'),
(2, 'wangyu14'),
(3, 'yuanyuan06');

CREATE TABLE class_test (class_name VARCHAR(64))
DISTRIBUTED BY HASH(class_name) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO class_test VALUES
('Suzi'), ('Suzi'),
('Ben'), ('Ben'),
('Henry'), ('Henry'),
(NULL);
```

直接使用 `FIELD` 查找位置(基于 1 的索引):

```sql
SELECT FIELD(2, 3, 1, 2, 5);
```

```text
+----------------------+
| FIELD(2, 3, 1, 2, 5) |
+----------------------+
| 3 |
+----------------------+
```

```sql
SELECT k1, k7 FROM baseall WHERE k1 IN (1,2,3) ORDER BY FIELD(k1,2,1,3);
```
Expand All @@ -51,6 +87,7 @@
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
Expand All @@ -74,6 +111,7 @@
| Ben |
| Suzi |
| Suzi |
| NULL |
+------------+
```

Expand All @@ -93,4 +131,4 @@
| Henry |
| Henry |
+------------+
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check warning on line 1 in i18n/zh-CN/docusaurus-plugin-content-docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-section-order

SQL function docs must contain sections in this order%3A Description%2C Syntax%2C Parameters%2C Return Value%2C Example. Missing%3A Example. Owner%3A @zclllyybb

Check warning on line 1 in i18n/zh-CN/docusaurus-plugin-content-docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

seo-title-duplicate

Rendered SEO title is duplicated across indexable pages%3A "FIELD - Apache Doris". Add a version%2C locale%2C or page-specific qualifier. Owner%3A @zclllyybb
{
"title": "FIELD",
"language": "zh-CN",
Expand All @@ -9,14 +9,14 @@
## 描述

返回 `<expr>` 在参数列表中的位置(基于 1 的索引),常用于 `ORDER BY` 子句中实现自定义排序。如果 `<expr>` 不在参数列表中,或 `<expr>` 为 `NULL`,则返回 `0`。在自定义排序中,不在参数列表中的数据会被排到最前面,可通过 `asc` 或 `desc` 控制整体排序顺序;对于 `NULL` 值,可以使用 `nulls first` 或 `nulls last` 控制排序顺序。

Check warning on line 12 in i18n/zh-CN/docusaurus-plugin-content-docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-parameters-table

Parameters section must be a Markdown table with Parameter and Description columns. Owner%3A @zclllyybb
## 语法

```sql
FIELD(<expr>, <param> [, ...])
```

## 参数

Check warning on line 19 in i18n/zh-CN/docusaurus-plugin-content-docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-return-type

Return Value section must state the returned type. Owner%3A @zclllyybb

| 参数 | 说明 |
|------------|-----------------------|
Expand All @@ -29,6 +29,42 @@

## 举例

```sql
-- setup
CREATE TABLE baseall (k1 INT, k7 VARCHAR(64))
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO baseall VALUES
(1, 'wangjing04'),
(2, 'wangyu14'),
(3, 'yuanyuan06');

CREATE TABLE class_test (class_name VARCHAR(64))
DISTRIBUTED BY HASH(class_name) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO class_test VALUES
('Suzi'), ('Suzi'),
('Ben'), ('Ben'),
('Henry'), ('Henry'),
(NULL);
```

直接使用 `FIELD` 查找位置(基于 1 的索引):

```sql
SELECT FIELD(2, 3, 1, 2, 5);
```

```text
+----------------------+
| FIELD(2, 3, 1, 2, 5) |
+----------------------+
| 3 |
+----------------------+
```

```sql
SELECT k1, k7 FROM baseall WHERE k1 IN (1,2,3) ORDER BY FIELD(k1,2,1,3);
```
Expand All @@ -51,6 +87,7 @@
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
Expand All @@ -74,6 +111,7 @@
| Ben |
| Suzi |
| Suzi |
| NULL |
+------------+
```

Expand All @@ -93,4 +131,4 @@
| Henry |
| Henry |
+------------+
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check warning on line 1 in versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

seo-title-duplicate

Rendered SEO title is duplicated across indexable pages%3A "FIELD - Apache Doris". Add a version%2C locale%2C or page-specific qualifier. Owner%3A @zclllyybb
{
"title": "FIELD",
"language": "en",
Expand All @@ -17,7 +17,7 @@
FIELD(<expr>, <param> [, ...])
```

## Parameters

Check notice on line 20 in versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-return-type

Return Value section must state the returned type. Owner%3A @zclllyybb

| Parameter | Description |
|------------|---------------------------------------------------------|
Expand All @@ -25,13 +25,35 @@
| `<param>` | A sequence of values to compare against `<expr>`. |

## Return Value

Check notice on line 28 in versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-example-output

Every fenced sql query in Example must be followed by a fenced text block with expected output. Owner%3A @zclllyybb
- Returns the position (1-based index) of `<expr>` in the list of `<param>` values.
- If `<expr>` is not found, returns `0`.
- If `<expr>` is `NULL`, returns `0`.

## Examples

```sql
-- setup
CREATE TABLE baseall (k1 INT, k7 VARCHAR(64))
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO baseall VALUES
(1, 'wangjing04'),
(2, 'wangyu14'),
(3, 'yuanyuan06');

CREATE TABLE class_test (class_name VARCHAR(64))
DISTRIBUTED BY HASH(class_name) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO class_test VALUES
('Suzi'), ('Suzi'),
('Ben'), ('Ben'),
('Henry'), ('Henry'),
(NULL);
```

```sql
SELECT FIELD(2, 3, 1, 2, 5);
```
Expand All @@ -58,6 +80,8 @@
+------+------------+
```

Custom string ordering. `FIELD` returns `0` for values not in the list — including `NULL`, which is why the `NULL` row sorts to the top in the ascending default:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry');
```
Expand All @@ -66,11 +90,52 @@
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
| Ben |
| Henry |
| Henry |
+------------+
```

Descending — `NULL` (FIELD = 0) is now last:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry') DESC;
```

```text
+------------+
| class_name |
+------------+
| Henry |
| Henry |
| Ben |
| Ben |
| Suzi |
| Suzi |
| NULL |
+------------+
```

You can also use `NULLS FIRST` / `NULLS LAST` to control `NULL` placement independently of the sort direction:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry') NULLS FIRST;
```

```text
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
| Ben |
| Henry |
| Henry |
+------------+
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

Check notice on line 1 in versioned_docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

i18n-sync-locale-candidate

Japanese docs are report-only. Generate a candidate translation from the changed files and merge it only after human review. Owner%3A @zclllyybb

Check warning on line 1 in versioned_docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

seo-title-duplicate

Rendered SEO title is duplicated across indexable pages%3A "FIELD - Apache Doris". Add a version%2C locale%2C or page-specific qualifier. Owner%3A @zclllyybb
{
"title": "FIELD",
"language": "en",
Expand All @@ -17,7 +17,7 @@
FIELD(<expr>, <param> [, ...])
```

## Parameters

Check warning on line 20 in versioned_docs/version-3.x/sql-manual/sql-functions/scalar-functions/other-functions/field.md

View workflow job for this annotation

GitHub Actions / Build Check

sql-function-return-type

Return Value section must state the returned type. Owner%3A @zclllyybb

| Parameter | Description |
|------------|---------------------------------------------------------|
Expand All @@ -32,6 +32,28 @@

## Examples

```sql
-- setup
CREATE TABLE baseall (k1 INT, k7 VARCHAR(64))
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO baseall VALUES
(1, 'wangjing04'),
(2, 'wangyu14'),
(3, 'yuanyuan06');

CREATE TABLE class_test (class_name VARCHAR(64))
DISTRIBUTED BY HASH(class_name) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO class_test VALUES
('Suzi'), ('Suzi'),
('Ben'), ('Ben'),
('Henry'), ('Henry'),
(NULL);
```

```sql
SELECT FIELD(2, 3, 1, 2, 5);
```
Expand All @@ -58,6 +80,8 @@
+------+------------+
```

Custom string ordering. `FIELD` returns `0` for values not in the list — including `NULL`, which is why the `NULL` row sorts to the top in the ascending default:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry');
```
Expand All @@ -66,11 +90,52 @@
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
| Ben |
| Henry |
| Henry |
+------------+
```

Descending — `NULL` (FIELD = 0) is now last:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry') DESC;
```

```text
+------------+
| class_name |
+------------+
| Henry |
| Henry |
| Ben |
| Ben |
| Suzi |
| Suzi |
| NULL |
+------------+
```

You can also use `NULLS FIRST` / `NULLS LAST` to control `NULL` placement independently of the sort direction:

```sql
SELECT class_name FROM class_test ORDER BY FIELD(class_name, 'Suzi', 'Ben', 'Henry') NULLS FIRST;
```

```text
+------------+
| class_name |
+------------+
| NULL |
| Suzi |
| Suzi |
| Ben |
| Ben |
| Henry |
| Henry |
+------------+
```
```
Loading