在PostgreSQL中,存储列表的最佳方式是什么?我需要使用哪种字段类型?需要对其进行序列化吗?
以下是我想要存储的数据示例:
id(数字),name(文本),events(列表)
1, john connor, ["aaa","bbb","ccc"]
2, jack bush, ["ttt","hhh","lll"]
...
在PostgreSQL中,存储列表的最佳方式是什么?我需要使用哪种字段类型?需要对其进行序列化吗?
以下是我想要存储的数据示例:
id(数字),name(文本),events(列表)
1, john connor, ["aaa","bbb","ccc"]
2, jack bush, ["ttt","hhh","lll"]
...
[]
声明任意大小的数组,或者用[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}
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