如何在postgresql中查找数组的大小

时间:2022-01-16 22:17:30

Is there any way to find a size of an array?

有没有办法找到一个数组的大小?

For Example,

例如,

CREATE TABLE example (id integer[]) ;

INSERT INTO exam VALUES ( '{}');

INSERT INTO exam VALUES ( '{5,6,7}');

From this, is there any possibilities to get a result like following,

从这个,有没有可能获得如下结果,

size

0

3

2 个解决方案

#1


39  

It's trivial reading docs:

这是一个微不足道的阅读文档:

SELECT array_length(id, 1) FROM example;

#2


31  

As vyegorov mentioned, array_length will do the trick. Or if you know that the array is 1-dimensional (which is likely) and are running PostgreSQL 9.4 or higher, you can use cardinality:

正如vyegorov所提到的,array_length将起到作用。或者,如果您知道该数组是1维(可能)并且正在运行PostgreSQL 9.4或更高版本,则可以使用基数:

SELECT cardinality(id) FROM example;

#1


39  

It's trivial reading docs:

这是一个微不足道的阅读文档:

SELECT array_length(id, 1) FROM example;

#2


31  

As vyegorov mentioned, array_length will do the trick. Or if you know that the array is 1-dimensional (which is likely) and are running PostgreSQL 9.4 or higher, you can use cardinality:

正如vyegorov所提到的,array_length将起到作用。或者,如果您知道该数组是1维(可能)并且正在运行PostgreSQL 9.4或更高版本,则可以使用基数:

SELECT cardinality(id) FROM example;