Postgresql常用函数整理

一、字符串函数

1、函数:string || string(string || non-string)

说明:字符串(或与非字符串)连接

示例:

 

2、函数:char_length(string)

说明:计算字符串中字符个数

示例:

 

3、函数:overlay(string placing string from int [for int])

说明:替换字符串中任意长度的子字串为新字符串

示例:

 

注:from 3 to 4 表示从第3个字节起开始,向后再算4个字节

4、函数:position(substring in string)

说明:子串在一字符串中的位置

示例:

 

5、函数:substring(string [from int] [for int])

说明:截取任意长度的子字符串

示例:

 

注:from 2 to 5 表示从第2个字节起开始,向后再算5个字节

6、函数:substring(string from pattern)

说明:利用正则表达式对一字符串进行任意长度的字串的截取

示例:

 

注:从后向前截取,一个点表示一位,如果字段内容小于截取长度,值为NULL

7、函数:trim([leading| trailing |both][ characters ]from string )

说明:从字符串 string 的开头/结尾/两边删除只包含characters 中字符 (缺省是空白)的最长的字符串

示例:

 

8、函数:lower( string )

说明:把字符串转化为小写

示例:

 

9、函数:upper( string )

说明:把字符串转化为大写

示例:

 

10、函数:bit_length(string)

说明:计算字符串位数(bits)

示例:

 

二、聚合函数

1、函数:string_agg( expression ,delimiter )

说明:输入值连接成为一个字符串,用分隔符分开

示例:

2、函数:json_agg( expression )

说明:聚合值作为JSON数组

示例:

 

json_array_elements  函数 示例:

select json_array_elements ('

{"id": "676a13d3-0225-4431-b858-678c3cfeab74", "weight": "1", "quantity": "9999999"},
{"id": "111a13d3-0225-4431-b858-678c3cfeab75", "weight": "2", "quantity": "33"},
{"id": "111a13d3-0225-4431-b858-678c3cfea999", "weight": "3", "quantity": "11"}
]
')

我们看到json数据被分离成三条记录,这时我们就可以对其进行查询操作,

比如查询是否包含了weight=3的数据。

select * from json_array_elements ('

{"id": "676a13d3-0225-4431-b858-678c3cfeab74", "weight": "1", "quantity": "9999999"},
{"id": "111a13d3-0225-4431-b858-678c3cfeab75", "weight": "2", "quantity": "33"},
{"id": "111a13d3-0225-4431-b858-678c3cfea999", "weight": "3", "quantity": "11"}
]
') as jae
where jae::jsonb->>'weight' = '3' 

#输出:

查询数据库内的表:

SELECT gp._id, jsonb_array_elements ( gp.full_id )->>0 AS "full_id" FROM comm_org_group gp

转载至-https://www.cnblogs.com/keynotes/p/8430310.html

 

原文地址:https://www.cnblogs.com/zxy-come-on/p/14272372.html