Skip to content
Database

使用数组

Postgres 支持灵活的 数组类型。这些数组在 Supabase 控制面板和 JavaScript API 中也同样支持。

🌐 Postgres supports flexible array types. These arrays are also supported in the Supabase Dashboard and in the JavaScript API.

创建一个有数组列的表 #

🌐 Create a table with an array column

创建一个带有文本数组(字符串数组)的测试表:

🌐 Create a test table with a text array (an array of strings):

  1. 在仪表板中转到表格编辑器页面。
  2. 点击 新建表 并创建一个名为 arraytest 的表。
  3. 点击保存
  4. 点击 新建列 并创建一个名为 textarray、类型为 text,并选择 定义为数组 的列。
  5. 点击保存

插入一个带数组值的记录 #

🌐 Insert a record with an array value

  1. 在仪表板中转到表格编辑器页面。
  2. 选择 arraytest 表。
  3. 点击 插入行 并添加 ["Harry", "Larry", "Moe"]
  4. 点击 保存。

查看结果 #

🌐 View the results

  1. 在仪表板中转到表格编辑器页面。
  2. 选择 arraytest 表。

你应该看看:

🌐 You should see:

1
| id | textarray |
2
| --- | ----------------------- |
3
| 1 | ["Harry","Larry","Moe"] |

查询数组数据 #

🌐 Query array data

Postgres 使用从 1 开始的索引(例如,textarray[1] 是数组中的第一个元素)。

🌐 Postgres uses 1-based indexing (e.g., textarray[1] is the first item in the array).

要从数组中选择第一个元素并获取数组的总长度:

🌐 To select the first item from the array and get the total length of the array:

1
SELECT textarray[1], array_length(textarray, 1) FROM arraytest;

returns:

1
| textarray | array_length |
2
| --------- | ------------ |
3
| Harry | 3 |

资源 #

🌐 Resources