postgresql使用过程中字段转换

1、interval类型字段换算成时分秒

date_part('hour', visit_times)*60*60+date_part('minute', visit_times)*60+date_part('seconds', visit_times)

2、计算时间差

to_timestamp(max(ddd),'HH24:MI:SS') -to_timestamp(min(ddd),'HH24:MI:SS')

3、查询jsonb字段中的key对应的值

(detail_jsonb->>'date')::varchar
--detail_jsonb为jsonb类型的字段名,date为其中的key

4、update效率的问题

update table1 set column1=(select column1 from table2) where table2.column2=table1.column2;
update table1 set table1.column1=table2.column1 from (select column1 from table2) where table2.column2=table1.column2;

遇到以上情况,优选第二种方式进行update操作。

5、自增序列取值

nextval('table_seq')


6、和字符串处理相关的函数

函数:string || string 
说明:String concatenation 字符串连接操作
例子:select 'Post' || 'greSQL'; = PostgreSQL

 

函数:string || non-string or non-string || string
说明:String concatenation with one non-string input 字符串与非字符串类型进行连接操作
例子:select 'Value: ' || 42; = Value: 42

 

函数:bit_length(string)
说明:Number of bits in string 计算字符串的位数
例子:select bit_length('pmars') = 40

 

函数:char_length(string) or character_length(string)
说明:Number of characters in string 计算字符串中字符个数
例子:select char_length('pmars'); = 5

 

函数:lower(string)
说明:Convert string to lower case 转换字符串为小写
例子:select lower('PmArS'); = "pmars"

 

函数:octet_length(string)
说明:Number of bytes in string 计算字符串的字节数
例子:select octet_length('我是pmars'); = 11  select octet_length('我');  = 3


函数:overlay(string placing string from int [for int])
说明:Replace substring 替换字符串中任意长度的子字串为新字符串
例子:select overlay('I am pmars' placing 'ming' from 6 for 5); = "I am ming"


函数:position(substring in string)
说明:Location of specified substring 子串在一字符串中的位置
例子&#x

你可能感兴趣的:(postgresql)