PostgreSQL 内置函数(聚合函数)

  • COUNT 函数:用于计算数据库表中的行数。
  • MAX 函数:用于查询某一特定列中最大值。
  • MIN 函数:用于查询某一特定列中最小值。
  • AVG 函数:用于计算某一特定列中平均值。
  • SUM 函数:用于计算数字列所有值的总和。
  • ARRAY 函数:用于输入值(包括null)添加到数组中。
  • Numeric 函数:完整列出一个 SQL 中所需的操作数的函数。
  • String 函数:完整列出一个 SQL 中所需的操作字符的函数。

PostgreSQL 数学函数

abs(x) —>绝对值 abs(-17.4) —>17.4
cbrt(double) —>立方根 cbrt(27.0) —>3
ceil(double/numeric) —>不小于参数的最小的整数 ceil(-42.8) —>-42
floor(double/numeric) —>不大于参数的最大整数 floor(-42.8) —>-43
mod(y, x) —>取余数 mod(9,4) 1
random() —>0.0到1.0之间的随机数值 random()
sqrt(double/numeric) —>平方根 sqrt(2.0) —>1.4142135623731
trunc(double/numeric) —>截断(向零靠近) trunc(42.8) —>42
trunc(v numeric, s int) —>截断为s小数位置的数字 trunc(42.438,2) —>42.43

PostgreSQL 字符串函数和操作符

string 丨丨 string —>字串连接 ‘Post’ 丨丨 ‘greSQL’ —>PostgreSQL
lower(string) —>把字串转化为小写 lower(‘TOM’) —>tom
upper(string) —>把字串转化为大写。upper(‘tom’) —>TOM
substring(string from int for int]) —>抽取子字串 substring(‘Thomas’ from 2 for 3) —>hom
substr(string, from int for int) —>抽取子字串。 substr(‘alphabet’, 3, 2) —> ph
length(string text) —>string中字符的数目 length(‘jose’) —>4

PostgreSQL 类型转换相关函数

to_char(int, text) —>整型转换为字符串 to_char(125, ‘999’)
to_char(double precision, text) —>双精度转换为字符串 to_char(125.8::real, ‘999D9’)
to_char(numeric, text) —>数字转换为字符串 to_char(-125.8, ‘999D99S’)
to_date(text, text) —>字符串转换为日期 to_date(‘05 Dec 2000’, ‘DD Mon YYYY’)
to_number(text, text) —>转换字符串为数字 to_number(‘12,454.8-’, ‘99G999D9S’)
to_timestamp(text, text) —>转换为指定的时间格式 time zone convert string to time stamp to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐