使用数组
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):
- 在仪表板中转到表格编辑器页面。
- 点击 新建表 并创建一个名为
arraytest的表。 - 点击保存。
- 点击 新建列 并创建一个名为
textarray、类型为text,并选择 定义为数组 的列。 - 点击保存。
插入一个带数组值的记录 #
🌐 Insert a record with an array value
- 在仪表板中转到表格编辑器页面。
- 选择
arraytest表。 - 点击 插入行 并添加
["Harry", "Larry", "Moe"]。 - 点击 保存。
查看结果 #
🌐 View the results
- 在仪表板中转到表格编辑器页面。
- 选择
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:
1SELECT textarray[1], array_length(textarray, 1) FROM arraytest;returns:
1| textarray | array_length |2| --------- | ------------ |3| Harry | 3 |资源 #
🌐 Resources