在 postgresql 中存储列表的最佳方法是什么?我必须使用什么样的字段?我必须序列化它吗?
这是我要存储的数据示例:
id (number), name (text), events (list)
1, john connor, ["aaa","bbb","ccc"]
2, jack bush, ["ttt","hhh","lll"]
...
在 postgresql 中存储列表的最佳方法是什么?我必须使用什么样的字段?我必须序列化它吗?
这是我要存储的数据示例:
id (number), name (text), events (list)
1, john connor, ["aaa","bbb","ccc"]
2, jack bush, ["ttt","hhh","lll"]
...
使用数组怎么样?它们被声明[]为任何大小和[3]具有 3 个单元格的数组。
CREATE TABLE test1 (
id integer,
name text,
events text[]);
INSERT into test1 (id, name, events)
values(1, 'john connor', '{"aaa", "bbb","ccc"}'),
(2, 'jack bush', '{"ttt", "hhh","lll"}');
输入数组时,所有内容都必须用大括号括起来,并且大括号必须用单引号括起来。最后,数组中的字符串必须使用双引号。
表格如下所示:
id name events
1 john connor {aaa,bbb,ccc}
2 jack bush {ttt,hhh,lll}
使用 json 示例
select
json_build_object(
user_id,
json_build_object(
'user_name',
table.user_name
'aaa',
table.aaa,
'bbb',
table.bbb,
'ccc',
table.ccc
)
)
from table