string_to_array函数和ANY一起使用用法select code from ods.my_table twhere id=374;当我查询出来的结构集为: 1,2,3//但是我有需要把他们拆分成一个数组进行处理,可以通过 string_to_array函数select string_to_array(code,’,’) from ods.my_table t where id=374;结果集为:{1,2,3}//查询t_road_workorder_info表,先把改字段转成一个数组,然后使用ANY运算符//只要改字段包含一个1,就返回true,当where条件为true的时候,该id字段就会查询出来了;SELECTid FROM t_road_workorder_info WHERE '1' = any(string_to_array(code,','));-- any和string_to_array合并使用select '1' = any(string_to_array(array_to_string(array(select stu_id from student),','),','));-- array_to_string用法-- array() 把查询出来的name字段转换为array数组-- array_to_string(’数组‘,'/')把数组转化为字符串,并用‘,'连接(使用提供的分隔符连接数组元素)select stu_name,array_to_string(array(select stu_name from student) ,',') from student;-- string_to_array用法将字符串拆分为数组-- 第一个参数为需要转换的字符串-- 第二个参数为字符串中每个数组的值的分隔符select string_to_array('1,2,3',',');-- any运算符-- 检查数组【1,2,3】是否包含一个值为2的元素select 2 = any(array[1,2,3]);-- 检查数组【1,2,3】是否包含一个值大于3的元素,select 3 < any(array[1,2,3]);
-- over()里头的分组以及排序的执行晚于where,group by,order by的执行-- 这样结果是先按照descri分组,在按照stu_id进行排序,第一列是序列号select row_number() over(partition by descri order by stu_id),descri,stu_id from student;
运行结果:
-- 先按照descri分组,在按照stu_id进行排序,然后赛选出rank为1的记录select* from (select row_number() over(partition by descri order by stu_id)rank,descri,stu_id from student) tmp where tmp.rank = '1';
-- 空值替换函数select stu_name,coalesce(stu_name,'未知') from student;
-- 字符串连接函数-- pgsql的三种字符连接函数,将多列的字段连接成一条数据-- array_agg(),string_agg(),xmlagg()分别处理数组,字符串,xml文档-- array_agg字符连接后是json格式select array_agg(stu_name order by stu_age desc),descrifrom student group by descri;
--string_agg可以指定分隔符select string_agg(stu_name,',' ORDER BY stu_name),descrifrom student group bydescri;
`-- 格式转换符显示转换,利用双冒号进行格式转换,字段名或数值::数值类型select stu_id::int8 as id from student;
-- 利用数据转换函数cast进行转换, cast(字段名或数值 as 数据类型)-- 将文本‘123’转化为int8类型select cast('123' as int8) num;select cast(stu_id as int) id from student;
-- 将时间戳转为指定格式select stu_time,to_char(stu_time,'yyyy-MM-dd') time from student;
-- 文本转整数SELECT CAST( '123' AS int4 );-- 文本转浮点数字SELECT CAST( '123.34' AS DECIMAL );SELECT CAST( '123.34' AS NUMERIC );-- 数字转文本SELECT CAST( 123 AS VARCHAR );--可变字符串SELECT CAST( - 123 AS CHAR ( 2 ) );-- 固定字符串,进行截断,将-123转为'-1'SELECT CAST( - 123 AS CHAR ( 6 ) );-- 固定字符串,进行空格填充,将-123转为'-123'SELECT CAST( 124.94 AS TEXT );--可变字符串,将124.94转为'124.94'SELECTto_char( 124.94, '999D9' );--将124.94转为'124.9',遵循四舍五入SELECTto_char( 124.94, 'FM999.99' );--将124.94转为'124.94'SELECTto_char( - 124.94, 'FM9999999.99' );--将-124.94转为'-124.94'SELECTto_char( - 124.94, 'FM9999999.990' );--将-124.94转为'-124.940'SELECTto_char( 124, '00000' );--左端用零补齐凑够5位,将124转为'00124'SELECTto_char( 124, '99999' );--左端用空格补齐凑够5位,将124转为'124'SELECTto_char( - 124.945, 'FM999' );--只显示整数部分,遵循四舍五入-- 时间戳(timestamp)转日期(date)SELECT CAST( now( ) AS DATE );--普通日期模式-- 时间戳(timestamp)转文本SELECT CAST( now( ) AS TEXT );--不指定输出格式SELECTto_char( now( ), 'yyyy-mm-dd' );--指定输出格式;-- 文本转日期(date)SELECTto_date( '2012-01-01', 'yyyy-mm-dd' );-- 文本转时间戳(TIMESTAMP)SELECTto_timestamp( '2012-01-01 12:02:01', 'yyyy-mm-dd HH24:MI:SS' );
-- array_to_string用法-- array() 把查询出来的name字段转换为array数组-- array_to_string(’数组‘,'/')把数组转化为字符串,并用‘,'连接(使用提供的分隔符连接数组元素)select stu_name,array_to_string(array(select stu_name from student) ,',') from student;
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’)
-- over()里头的分组以及排序的执行晚于where,group by,order by的执行-- 这样结果是先按照descri分组,在按照stu_id进行排序,第一列是序列号select row_number() over(partition by descri order by stu_id),descri,stu_id from student;-- 先按照descri分组,在按照stu_id进行排序,然后赛选出rank为1的记录select* from (select row_number() over(partition by descri order by stu_id)rank,descri,stu_id from student) tmp where tmp.rank = '1';-- 将时间戳转为指定格式select stu_time,to_char(stu_time,'yyyy-MM-dd') time from student;-- 空值替换函数select stu_name,coalesce(stu_name,'未知') from student;-- 字符串连接函数-- pgsql的三种字符连接函数,将多列的字段连接成一条数据-- array_agg(),string_agg(),xmlagg()分别处理数组,字符串,xml文档-- array_agg字符连接后是json格式select array_agg(stu_name order by stu_age desc),descrifrom student group by descri;--string_agg可以指定分隔符select string_agg(stu_name,',' ORDER BY stu_name),descrifrom student group bydescri;-- 格式转换符显示转换,利用双冒号进行格式转换,字段名或数值::数值类型select stu_id::int8 as id from student;-- 利用数据转换函数cast进行转换, cast(字段名或数值 as 数据类型)-- 将文本‘123’转化为int8类型select cast('123' as int8) num;select cast(stu_id as int) id from student;select score,trunc(score) from student;-- 逻辑操作符and or notselect concat(t.stu_name,c.class_name)from student t,class c where t.stu_id = c.stu_id;--字符串拼接select stu_name||score as stu_scorefrom student;-- 将字符串转化为小写select lower('Hello');-- 替换子串,for后面是被替换的位数select overlay('Hexxx,word' placing 'llo' from 3 for 4);-- 提取子串select substring('hello' from 1 for 3);select substr('Hello',1,3)-- 将除了第一个参数外的其它参数用分隔符串串接在一起,第一个参数被用作分割字符串。null参数被忽略select concat_ws(',','abcde',2,null,22)-- 将string中出现的所有子串from替换为子串toselect replace('hello','ello','is');select now();-- array_to_string用法-- array() 把查询出来的name字段转换为array数组-- array_to_string(’数组‘,'/')把数组转化为字符串,并用‘,'连接(使用提供的分隔符连接数组元素)select stu_name,array_to_string(array(select stu_name from student) ,',') from student;-- string_to_array用法将字符串拆分为数组-- 第一个参数为需要转换的字符串-- 第二个参数为字符串中每个数组的值的分隔符select string_to_array('1,2,3',',');-- any运算符-- 检查数组【1,2,3】是否包含一个值为2的元素select 2 = any(array[1,2,3]);-- 检查数组【1,2,3】是否包含一个值大于3的元素,select 3 < any(array[1,2,3]);-- any和string_to_array合并使用select '1' = any(string_to_array(array_to_string(array(select stu_id from student),','),','));-- 条件类函数select stu_id, case when stu_name = '张三' then '大大' else stu_name end as stuName, score from student;-- nullif当value1和value2相等时,nullif返回一个空值,否则它返回value1select nullif(1,2)-- 查看所有函数名,返回类型,及参数个数SELECTpg_proc.proname AS "函数名称",pg_type.typname AS "返回值数据类型",pg_proc.pronargs AS "参数个数"FROMpg_procJOIN pg_type ON (pg_proc.prorettype = pg_type.oid)-- WHERE pronamespace = (SELECT pg_namespace.oid FROM pg_namespace WHERE nspname = '模式')
-- 逻辑操作符and or not
select concat(t.stu_name,c.class_name)from student t,class c where t.stu_id = c.stu_id;
字符串函数
日期,时间类函数****
数组函数
条件类函数
https://blog.csdn.net/qq_41780234/article/details/125547734?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168161182116800180642029%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168161182116800180642029&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-2-125547734-null-null.142v83control,239v2insert_chatgpt&utm_term=pgsql%E4%B8%AD%E7%9A%84%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E6%80%BB%E7%BB%93&spm=1018.2226.3001.4187