array_prepend() 是一个用于将元素添加到数组开头的系统函数。
array_prepend() 在 PostgreSQL 7.4 中添加。
示例
有关 array_prepend() 的基本用法示例
postgres=# SELECT array_prepend(1, ARRAY[2,3]);
array_prepend
---------------
{1,2,3}
(1 row)
postgres=# SELECT array_prepend('foo', ARRAY['bar','baz']);
array_prepend
---------------
{foo,bar,baz}
(1 row)
请注意,anyelement || anyarray 操作符是等效的。
postgres=# SELECT 1 || ARRAY[2,3];
?column?
----------
{1,2,3}
(1 row)
无法将任意数据类型添加到特定类型的数组中。
postgres=# SELECT array_prepend(1, ARRAY['bar','baz']);
ERROR: function array_prepend(integer, text[]) does not exist
LINE 1: SELECT array_prepend(1, ARRAY['bar','baz']);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts
参考资料
- PostgreSQL documentation: 数组函数
