1.Base基础/3.Icon图标/操作/search备份
1.Base基础/3.Icon图标/操作/search备份
EN
文档
AntDB简介
系统架构
最佳实践
参考指南
  • 文档首页 /
  • 参考指南 /
  • SQL语法 /
  • 常用函数

常用函数

更新时间:2024-07-24 15:10:26

下面列举了AntDB常用的一些函数。

数学函数

下面是AntDB中提供的数学函数列表,需要说明的是,这些函数中有许多都存在多种形式,区别只是参数类型不同。除非特别指明,任何特定形式的函数都返回和它的参数相同的数据类型。

函数返回类型描述例子结果
abs(x)(和输入相同)绝对值abs(-17.4)17.4
cbrt(dp)dp立方根cbrt(27.0)3
ceil(dp or numeric)(和输入相同)不小于参数的最近的整数ceil(-42.8)-42
ceiling(dp or numeric)(和输入相同)不小于参数的最近的整数(ceil的别名)ceiling(-95.3)-95
degrees(dp)dp把弧度转为角度degrees(0.5)28.6478897565412
div(y numeric, x numeric)numericy/x的整数商div(9,4)2
exp(dp or numeric)(和输入相同)指数exp(1.0)2.71828182845905
floor(dp or numeric)(和输入相同)不大于参数的最近的整数floor(-42.8)-43
ln(dp or numeric)(和输入相同)自然对数ln(2.0)0.693147180559945
log(dp or numeric)(和输入相同)以10为底的对数log(100.0)2
log10(dp or numeric)(和输入相同)以10为底的对数log10(100.0)2
log(b numeric, x numeric)numericb为底的对数log(2.0, 64.0)6.0000000000
mod(y, x)(和参数类型相同)y/x的余数mod(9,4)1
pi()dp“π”常数pi()3.14159265358979
power(a dp, b dp)dpab次幂power(9.0, 3.0)729
power(a numeric, b numeric)numericab次幂power(9.0, 3.0)729
radians(dp)dp把角度转为弧度radians(45.0)0.785398163397448
round(dp or numeric)(和输入相同)圆整为最接近的整数round(42.4)42
round(v numeric, s int)numeric圆整为s位小数数字round(42.4382, 2)42.44
scale(numeric)integer参数的精度(小数点后的位数)scale(8.41)2
sign(dp or numeric)(和输入相同)参数的符号(-1, 0, +1)sign(-8.4)-1
sqrt(dp or numeric)(和输入相同)平方根sqrt(2.0)1.4142135623731
trunc(dp or numeric)(和输入相同)截断(向零靠近)trunc(42.8)42
trunc(v numeric, s int)numeric截断为s位小数位置的数字trunc(42.4382, 2)42.43
width_bucket(op dp, b1 dp, b2 dp, count int)int返回一个桶号,这个桶是在一个柱状图中operand将被分配的那个桶,该柱状图有count个散布在范围b1到b2上的等宽桶。对于超过该范围的输入,将返回0或者count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(op numeric, b1 numeric, b2 numeric, count int)int返回一个桶号,这个桶是在一个柱状图中operand将被分配的那个桶,该柱状图有count个散布在范围b1到b2上的等宽桶。对于超过该范围的输入,将返回0或者count+1width_bucket(5.35, 0.024, 10.06, 5)3
width_bucket(operand anyelement, thresholds anyarray)int返回一个桶号,这个桶是在给定数组中operand 将被分配的桶,该数组列出了桶的下界。对于一个低于第一个下界的输入返回 0。thresholds数组必须被排好序, 最小的排在最前面,否则将会得到意想不到的结果width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2

随机函数

函数返回类型描述
random()dp范围 0.0 <= x < 1.0 中的随机值
setseed(dp)void为后续的random()调用设置种子(值为于 -1.0 和 1.0 之间,包括边界值)

三角函数

函数(弧度)函数(角度)描述
acos(x)acosd(x)反余弦
asin(x)asind(x)反正弦
atan(x)atand(x)反正切
atan2(y, x)atan2d(y, x)y/x的反正切
cos(x)cosd(x)余弦
cot(x)cotd(x)余切
sin(x)sind(x)正弦
tan(x)tand(x)正切

双曲函数

函数描述举例结果
sinh(x)双曲正弦sinh(0)0
cosh(x)双曲余弦cosh(0)1
tanh(x)双曲切线tanh(0)0
asinh(x)反双曲正弦asinh(0)0
acosh(x)反双曲余弦acosh(1)0
atanh(x)反双曲切线atanh(0)0

字符串函数

下面是 AntDB 中提供的字符串操作符列表:

函数返回类型描述例子结果
string 丨丨 stringtext字串连接'Post' 丨丨 'greSQL'PostgreSQL
bit_length(string)int字串里二进制位的个数bit_length('jose')32
char_length(string)int字串中的字符个数char_length('jose')4
convert(string using conversion_name)text使用指定的转换名字改变编码。convert('PostgreSQL' using iso_8859_1_to_utf8)'PostgreSQL'
lower(string)text把字串转化为小写lower('TOM')tom
octet_length(string)int字串中的字节数octet_length('jose')4
overlay(string placing string from int [for int])text替换子字串overlay('Txxxxas' placing 'hom' from 2 for 4)Thomas
position(substring in string)int指定的子字串的位置position('om' in 'Thomas')3
substring(string [from int] [for int])text抽取子字串substring('Thomas' from 2 for 3)hom
substring(string from pattern)text抽取匹配 POSIX 正则表达式的子字串substring('Thomas' from '…$')mas
substring(string from pattern for escape)text抽取匹配SQL正则表达式的子字串substring('Thomas' from '%#"o_a#"_' for '#')oma
trim([leading丨trailing 丨 both] [characters] from string)text从字串string的开头/结尾/两边/ 删除只包含characters(默认是一个空白)的最长的字串trim(both 'x' from 'xTomxx')Tom
upper(string)text把字串转化为大写。upper('tom')TOM
ascii(text)int参数第一个字符的ASCII码ascii('x')120
btrim(string text [, characters text])text从string开头和结尾删除只包含在characters里(默认是空白)的字符的最长字串btrim('xyxtrimyyx','xy')trim
chr(int)text给出ASCII码的字符chr(65)A
convert(string text, [src_encoding name,] dest_encoding name)text把字串转换为dest_encodingconvert( 'text_in_utf8', 'UTF8', 'LATIN1')以ISO 8859-1编码表示的text_in_utf8
initcap(text)text把每个单词的第一个子母转为大写,其它的保留小写。单词是一系列字母数字组成的字符,用非字母数字分隔。initcap('hi thomas')Hi Thomas
length(string text)intstring中字符的数目length('jose')4
lpad(string text, length int [, fill text])text通过填充字符fill(默认为空白),把string填充为长度length。 如果string已经比length长则将其截断(在右边)。lpad('hi', 5, 'xy')xyxhi
ltrim(string text [, characters text])text从字串string的开头删除只包含characters(默认是一个空白)的最长的字串。ltrim('zzzytrim','xyz')trim
md5(string text)text计算给出string的MD5散列,以十六进制返回结果。md5('abc')
repeat(string text, number int)text重复string number次。repeat('Pg', 4)PgPgPgPg
replace(string text, from text, to text)text把字串string里出现地所有子字串from替换成子字串to。replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
rpad(string text, length int [, fill text])text通过填充字符fill(默认为空白),把string填充为长度length。如果string已经比length长则将其截断。rpad('hi', 5, 'xy')hixyx
rtrim(string text [, character text])text从字串string的结尾删除只包含character(默认是个空白)的最长的字rtrim('trimxxxx','x')trim
split_part(string text, delimiter text, field int)text根据delimiter分隔string返回生成的第field个子字串(1 Base)。split_part('abc~@~def~@~ghi', '~@~', 2)def
strpos(string, substring)text声明的子字串的位置。strpos('high','ig')2
substr(string, from [, count])text抽取子字串。substr('alphabet', 3, 2)ph
to_ascii(text [, encoding])text把text从其它编码转换为ASCII。to_ascii('Karel')Karel
to_hex(number int/bigint)text把number转换成其对应地十六进制表现形式。to_hex(9223372036854775807)7fffffffffffffff
translate(string text, from text, to text)text把在string中包含的任何匹配from中的字符的字符转化为对应的在to中的字符。translate('12345', '14', 'ax')a23x5

数据类型格式化函数

函数返回类型描述例子
to_char(timestamp, text)text把时间戳转成字符串to_char(current_timestamp, 'HH12:MI:SS')
to_char(interval, text)text把间隔转成字符串to_char(interval '15h 2m 12s', 'HH24:MI:SS')
to_char(int, text)text把整数转成字符串to_char(125, '999')
to_char(double precision, text)text把实数或双精度转成字符串to_char(125.8::real, '999D9')
to_char(numeric, text)text把数字转成字符串to_char(-125.8, '999D99S')
to_date(text, text)date把字符串转成日期to_date('05 Dec 2000', 'DD Mon YYYY')
to_number(text, text)numeric把字符串转成数字to_number('12,454.8-', '99G999D9S')
to_timestamp(text, text)timestamp with time zone把字符串转成时间戳to_timestamp('05 Dec 2000', 'DD Mon YYYY')

日期/时间函数

函数返回类型描述例子结果
age(timestamp, timestamp)interval减去参数,生成一个使用年、月(而不是只用日)的“符号化”的结果age(timestamp '2001-04-10', timestamp '1957-06-13')43 年 9 月 27 日
age(timestamp)interval从current_date(在午夜)减去age(timestamp '1957-06-13')43 years 8 mons 3 days
clock_timestamp()timestamp with time zone当前日期和时间(在语句执行期间变化)
current_datedate当前日期
current_timetime with time zone当前时间(一天中的时间)
current_timestamptimestamp with time zone当前日期和时间(当前事务开始时)
date_part(text, timestamp)double precision获得子域(等价于extract)date_part('hour', timestamp '2001-02-16 20:38:40')20
date_part(text, interval)double precision获得子域(等价于extract)date_part('month', interval '2 years 3 months')3
date_trunc(text, timestamp)timestamp截断到指定精度date_trunc('hour', timestamp '2001-02-16 20:38:40')2001-02-16 20:00:00
date_trunc(text, timestamp with time zone, text)timestamp with time zone在指定的时区截断到指定的精度date_trunc('day', timestamptz '2001-02-16 20:38:40+00', 'Australia/Sydney')2001-02-16 13:00:00+00
date_trunc(text, interval)interval截断到指定精度date_trunc('hour', interval '2 days 3 hours 40 minutes')2 days 03:00:00
extract(field from timestamp)double precision获得子域extract(hour from timestamp '2001-02-16 20:38:40')20
extract(field from interval)double precision获得子域extract(month from interval '2 years 3 months')3
isfinite(date)boolean测试有限日期(不是+/-无限)isfinite(date '2001-02-16')true
isfinite(timestamp)boolean测试有限时间戳(不是+/-无限)isfinite(timestamp '2001-02-16 21:28:30')true
isfinite(interval)boolean测试有限间隔isfinite(interval '4 hours')true
justify_days(interval)interval调整间隔这样30天时间周期可以表示为月justify_days(interval '35 days')1 mon 5 days
justify_hours(interval)interval调整间隔这样24小时时间周期可以表示为日justify_hours(interval '27 hours')1 day 03:00:00
justify_interval(interval)interval使用justify_days和justify_hours调整间隔,使用额外的符号调整justify_interval(interval '1 mon -1 hour')29 days 23:00:00
localtimetime当前时间(一天中的时间)
localtimestamptimestamp当前日期和时间(当前事务的开始)
make_date(year int, month int, day int)date从年、月、日域创建日期make_date(2013, 7, 15)2013-07-15
make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)interval从年、月、周、日、时、分、秒域创建 intervalmake_interval(days => 10)10 days
make_time(hour int, min int, sec double precision)time从时、分、秒域创建时间make_time(8, 15, 23.5)08:15:23.5
make_timestamp(year int, month int, day int, hour int, min int, sec double precision)timestamp从年、月、日、时、分、秒域创建时间戳make_timestamp(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])timestamp with time zone从年、月、日、时、分、秒域创建带时区的时间戳。如果没有指定timezone, 则使用当前时区。make_timestamptz(2013, 7, 15, 8, 15, 23.5)2013-07-15 08:15:23.5+01
now()timestamp with time zone当前日期和时间(当前事务的开始)
statement_timestamp()timestamp with time zone当前日期和时间(当前语句的开始)
timeofday()text当前日期和时间(clock_timestamp,但是作为一个text字符串)
transaction_timestamp()timestamp with time zone当前日期和时间(当前事务的开始)
to_timestamp(double precision)timestamp with time zone把 Unix 时间(从 1970-01-01 00:00:00+00 开始的秒)转换成 timestampto_timestamp(1284352323)2010-09-13 04:32:03+0

网络地址函数

函数返回类型描述例子结果
abbrev(inet)text缩写显示格式文本abbrev(inet '10.1.0.0/16')10.1.0.0/16
abbrev(cidr)text缩写显示格式文本abbrev(cidr '10.1.0.0/16')10.1/16
broadcast(inet)inet网络广播地址broadcast('192.168.1.5/24')192.168.1.255/24
family(inet)int抽取地址族;4为 IPv4, 6为 IPv6family('::1')6
host(inet)text抽取 IP 地址为文本host('192.168.1.5/24')192.168.1.5
hostmask(inet)inet为网络构造主机掩码hostmask('192.168.23.20/30')0.0.0.3
masklen(inet)int抽取网络掩码长度masklen('192.168.1.5/24')24
netmask(inet)inet为网络构造网络掩码netmask('192.168.1.5/24')255.255.255.0
network(inet)cidr抽取地址的网络部分network('192.168.1.5/24')192.168.1.0/24
set_masklen(inet, int)inet为inet值设置网络掩码长度set_masklen('192.168.1.5/24', 16)192.168.1.5/16
set_masklen(cidr, int)cidr为cidr值设置网络掩码长度set_masklen('192.168.1.0/24'::cidr, 16)192.168.0.0/16
text(inet)text抽取 IP 地址和网络掩码长度为文本text(inet '192.168.1.5')192.168.1.5/32
inet_same_family(inet, inet)boolean地址是来自于同一个家族吗?inet_same_family('192.168.1.5/24', '::1')FALSE
inet_merge(inet, inet)cidr包括给定网络的最小网络inet_merge('192.168.1.5/24', '192.168.2.5/24')192.168.0.0/22

文本搜索函数

函数返回类型描述例子结果
array_to_tsvector(text[])tsvector把词位数组转换成tsvectorarray_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'
get_current_ts_config()regconfig获得默认文本搜索配置get_current_ts_config()english
length(tsvector)integertsvector中的词位数length('fat:2,4 cat:3 rat:5A'::tsvector)3
numnode(tsquery)integertsquery中词位外加操作符的数目numnode('(fat & rat) | cat'::tsquery)5
plainto_tsquery([ config regconfig , ] query text)tsquery产生tsquery但忽略标点符号plainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'
phraseto_tsquery([ config regconfig , ] query text)tsquery产生忽略标点搜索短语的tsqueryphraseto_tsquery('english', 'The Fat Rats')'fat' <-> 'rat'
websearch_to_tsquery([ config regconfig , ] query text)tsquery从一个Web搜索风格的查询产生tsquerywebsearch_to_tsquery('english', '"fat rat" or rat')'fat' <-> 'rat' | 'rat'
querytree(query tsquery)text获得一个tsquery的可索引部分querytree('foo & ! bar'::tsquery)'foo'
setweight(vector tsvector, weight "char")tsvector为vector的每一个元素分配权重setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvector, weight "char", lexemes text[])tsvector为lexemes中列出的vector的元素分配权重setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 'rat':5A
strip(tsvector)tsvector从tsvector中移除位置和权重strip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'
to_tsquery([ config regconfig , ] query text)tsquery规范化词并转换成tsqueryto_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'
to_tsvector([ config regconfig , ] document text)tsvector缩减文档文本成tsvectorto_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
to_tsvector([ config regconfig , ] document json(b))tsvector把该文档中的每个字符串值缩减成一个tsvector,然后将它们按在文档中的顺序串接起来形成一个tsvectorto_tsvector('english', '{"a": "The Fat Rats"}'::json)'fat':2 'rat':3
json(b)_to_tsvector([ config regconfig, ] document json(b), filter json(b))tsvector把filter指定的文档中的每个值缩减为一个tsvector,然后把它们按照文档中的顺序串接起来形成一个tsvector。filter是一个jsonb数组,它列举哪些种类的元素需要被包括在结果tsvector中。json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')'123':5 'fat':2 'rat':3
ts_delete(vector tsvector, lexeme text)tsvector从vector中移除给定的lexemets_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')'cat':3 'rat':5A
ts_delete(vector tsvector, lexemes text[])tsvector从vector中移除lexemes中词位的任何出现ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])'cat':3
ts_filter(vector tsvector, weights "char"[])tsvector从vector中只选择带有给定权重的元素ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A
ts_headline([ config regconfig, ] document text, query tsquery [, options text ])text显示一个查询匹配ts_headline('x y z', 'z'::tsquery)x y z
ts_headline([ config regconfig, ] document json(b), query tsquery [, options text ])text显示一个查询匹配ts_headline('{"a":"x y z"}'::json, 'z'::tsquery){"a":"x y z"}
ts_rank([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ])float4为查询排名文档ts_rank(textsearch, query)0.818
ts_rank_cd([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ])float4使用覆盖密度为查询排名文档ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query)2.01317
ts_rewrite(query tsquery, target tsquery, substitute tsquery)tsquery在查询内用substitute替换targetts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsquery, select text)tsquery使用来自一个SELECT的目标和替换者进行替换SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )
tsquery_phrase(query1 tsquery, query2 tsquery)tsquery制造搜索后面跟着query2的query1的查询(和<->操作符相同)tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'
tsquery_phrase(query1 tsquery, query2 tsquery, distance integer)tsquery制造查询来搜索在query1后面最大距离distance上跟着query2 的情况tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)'fat' <10> 'cat'
tsvector_to_array(tsvector)text[]把tsvector转换为词位数组tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector){cat,fat,rat}
tsvector_update_trigger()trigger用于自动tsvector列更新的触发器函数CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column()trigger用于自动tsvector列更新的触发器函数CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body)
unnest(tsvector, OUT lexeme text, OUT positions smallint[], OUT weights text)setof record把一个 tsvector 扩展成一组行unnest('fat:2,4 cat:3 rat:5A'::tsvector)(cat,{3},{D}) ...

JSON函数

下表为JSON 创建函数:

函数描述例子例子结果
to_json(anyelement) to_jsonb(anyelement)把该值返回为json或者jsonb。数组和组合 会被(递归)转换成数组和对象;对于不是数组和组合的值,如果有 从该类型到json的造型,造型函数将被用来执行该 转换;否则将产生一个标量值。对于任何不是数字、布尔、空值的标 量类型,将使用文本表达,在这种风格下它是一个合法的 json或者jsonb值。to_json('Fred said "Hi."'::text)"Fred said "Hi.""
array_to_json(anyarray [, pretty_bool])把数组作为一个 JSON 数组返回。一个 SQL 多维数组会成为一个数组 的 JSON 数组。如果pretty_bool为真,将在 第 1 维度的元素之间增加换行。array_to_json('{{1,5},{99,100}}'::int[])[[1,5],[99,100]]
row_to_json(record [, pretty_bool])把行作为一个 JSON 对象返回。如果pretty_bool为真,将在第1层元素之间增加换行。row_to_json(row(1,'foo')){"f1":1,"f2":"foo"}
json_build_array(VARIADIC "any") jsonb_build_array(VARIADIC "any")从一个可变参数列表构造一个可能包含异质类型的 JSON 数组。json_build_array(1,2,'3',4,5)[1, 2, "3", 4, 5]
json_build_object(VARIADIC "any") jsonb_build_object(VARIADIC "any")从一个可变参数列表构造一个 JSON 对象。通过转换,该参数列表由交替 出现的键和值构成。json_build_object('foo',1,'bar',2){"foo": 1, "bar": 2}
json_object(text[]) jsonb_object(text[])从一个文本数组构造一个 JSON 对象。该数组必须可以是具有偶数个成员的 一维数组(成员被当做交替出现的键/值对),或者是一个二维数组(每一个 内部数组刚好有 2 个元素,可以被看做是键/值对)。json_object('{a, 1, b, "def", c, 3.5}')````````json_object('{{a, 1},{b, "def"},{c, 3.5}}'){"a": "1", "b": "def", "c": "3.5"}
json_object(keys text[], values text[])````````jsonb_object(keys text[], values text[])json_object的这种形式从两个独立的数组得到键/值对。在其 他方面和一个参数的形式相同。json_object('{a, b}', '{1,2}'){"a": "1", "b": "2"}

数组函数

函数返回类型描述
array_append(anyarray, anyelement)anyarray向一个数组的末端追加一个元素
array_cat(anyarray, anyarray)anyarray连接两个数组
array_ndims(anyarray)int返回数组的维度数
array_dims(anyarray)text返回数组的维度的文本表示
array_fill(anyelement, int[], [, int[]])anyarray返回一个用提供的值和维度初始化好的数组,可以选择下界不为 1
array_length(anyarray, int)int返回被请求的数组维度的长度
array_lower(anyarray, int)int返回被请求的数组维度的下界
array_position(anyarray, anyelement [, int])int返回在该数组中从第三个参数指定的元素开始或者 第一个元素开始(数组必须是一维的)、第二个参数的 第一次出现的下标
array_positions(anyarray, anyelement)int[]返回在第一个参数给定的数组(数组必须是一维的)中, 第二个参数所有出现位置的下标组成的数组
array_prepend(anyelement, anyarray)anyarray向一个数组的首部追加一个元素
array_remove(anyarray, anyelement)anyarray从数组中移除所有等于给定值的所有元素(数组必须是一维的)
array_replace(anyarray, anyelement, anyelement)anyarray将每一个等于给定值的数组元素替换成一个新值
array_to_string(anyarray, text [, text])text使用提供的定界符和可选的空串连接数组元素
array_upper(anyarray, int)int返回被请求的数组维度的上界
cardinality(anyarray)int返回数组中元素的总数,如果数组为空则返回 0
string_to_array(text, text [, text])text[]使用提供的定界符和可选的空串将字符串划分成数组元素
unnest(anyarray)setof anyelement将一个数组扩展成一组行
unnest(anyarray, anyarray [, ...])setof anyelement, anyelement [, ...][把多维数组(可能是不同类型)扩展成一个行的集合。

示例

antdb=# select array_append(ARRAY[1,2], 3); array_append-------------- {1,2,3}(1 row)antdb=# select array_cat(ARRAY[1,2,3], ARRAY[4,5]);  array_cat------------- {1,2,3,4,5}(1 row)antdb=# select array_ndims(ARRAY[[1,2,3], [4,5,6]]); array_ndims-------------           2(1 row)antdb=# select array_dims(ARRAY[[1,2,3], [4,5,6]]); array_dims------------ [1:2][1:3](1 row)antdb=# select array_fill(7, ARRAY[3], ARRAY[2]);  array_fill--------------- [2:4]={7,7,7}(1 row)antdb=# select array_length(array[1,2,3], 1); array_length--------------            3(1 row)antdb=# select array_lower('[0:2]={1,2,3}'::int[], 1); array_lower-------------           0(1 row)antdb=# select array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon'); array_position----------------              2(1 row)antdb=# select array_positions(ARRAY['A','A','B','A'], 'A'); array_positions----------------- {1,2,4}(1 row)antdb=# select array_prepend(1, ARRAY[2,3]); array_prepend--------------- {1,2,3}(1 row)antdb=# select array_remove(ARRAY[1,2,3,2], 2); array_remove-------------- {1,3}(1 row)antdb=# select array_replace(ARRAY[1,2,5,4], 5, 3); array_replace--------------- {1,2,3,4}(1 row)antdb=# select array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*'); array_to_string----------------- 1,2,3,*,5(1 row)antdb=# select array_upper(ARRAY[1,8,3,7], 1); array_upper-------------           4(1 row)antdb=# select cardinality(ARRAY[[1,2],[3,4]]); cardinality-------------           4(1 row)antdb=# select string_to_array('xx~^~yy~^~zz', '~^~', 'yy'); string_to_array----------------- {xx,NULL,zz}(1 row)antdb=# select unnest(ARRAY[1,2]); unnest--------      1      2(2 rows)

范围函数

函数返回类型描述例子结果
lower(anyrange)范围的元素类型范围的下界lower(numrange(1.1,2.2))1.1
upper(anyrange)范围的元素类型范围的上界upper(numrange(1.1,2.2))2.2
isempty(anyrange)boolean范围为空?isempty(numrange(1.1,2.2))FALSE
lower_inc(anyrange)boolean下界包含在内?lower_inc(numrange(1.1,2.2))TRUE
upper_inc(anyrange)boolean上界包含在内?upper_inc(numrange(1.1,2.2))FALSE
lower_inf(anyrange)boolean下界无限?lower_inf('(,)'::daterange)TRUE
upper_inf(anyrange)boolean上界无限?upper_inf('(,)'::daterange)TRUE
range_merge(anyrange, anyrange)anyrange包含两个给定范围的最小范围range_merge('[1,2)'::int4range, '[3,4)'::int4range)[1,4)

聚集函数

函数参数类型返回类型部分模式描述
array_agg(expression)任何非数组类型参数类型的数组No输入值(包括空)被连接到一个数组
array_agg(expression)任意数组类型和参数数据类型相同No输入数组被串接到一个更高维度的数组中 (输入必须都具有相同的维度并且不能为空或者 NULL)
avg(expression)smallint, int, bigint、real、double precision、numeric或interval对于任何整数类型参数是numeric,对于一个浮点参数是double precision,否则和参数数据类型相同Yes所有非空输入值的平均值(算术平均)
bit_and(expression)smallint、int、bigint或bit与参数数据类型相同Yes所有非空输入值的按位与,如果没有非空值则结果是空值
bit_or(expression)smallint, int, bigint, or bit与参数数据类型相同Yes所有非空输入值的按位或,如果没有非空值则结果是空值
bool_and(expression)boolboolYes如果所有输入值为真则结果为真,否则为假
bool_or(expression)boolboolYes至少一个输入值为真时结果为真,否则为假
count(*)bigintYes输入的行数
count(expression)anybigintYesexpression值非空的输入行的数目
every(expression)boolboolYes等价于bool_and
json_agg(expression)anyjsonNo将值,包含空值,聚集成一个 JSON 数组
jsonb_agg(expression)anyjsonbNo把值,包含空值,聚合成一个 JSON 数组
json_object_agg(name, value)(any, any)jsonNo将名字/值对聚集成一个 JSON 对象,值可以为空,但不能是名字。
jsonb_object_agg(name, value)(any, any)jsonbNo把名字/值对聚合成一个 JSON 对象,值可以为空,但不能是名字。
max(expression)任意数组、数字、串、日期/时间、网络或者枚举类型,或者这些类型的数组与参数数据类型相同Yes所有非空输入值中expression的最大值
min(expression)任意数组、数字、串、日期/时间、网络或者枚举类型,或者这些类型的数组与参数数据类型相同Yes所有非空输入值中expression的最小值
string_agg(expression, delimiter)(text, text) 或 (bytea, bytea)与参数数据类型相同No非空输入值连接成一个串,用定界符分隔
sum(expression)smallint、int、 bigint、real、double precision、numeric、 interval或money对smallint或int参数是bigint,对bigint参数是numeric,否则和参数数据类型相同Yes所有非空输入值的expression的和
xmlagg(expression)xmlxmlNo连接 非空XML 值
问题反馈