下面列出来一些常用的SQL 语句:
字符串操作:https://www.cnblogs.com/alianbog/p/5656722.html
1. regexp_split_to_table(field_name,separator)
把某个字段的值 拆分成结果集
2.regexp_split_to_array(field_name,separator)
把某个字段的值 拆分成数组
3.array_length(regexp_split_to_array(field_name,separator) ,1)
计算拆分后数组的长度,1表示维度
4.SPLIT_PART(field_name,separator,1)
取出分割后的数组的第1的元素
5. SUBSTRING(name,LENGTH(name))
取出 name的最后一个字符
6. 数组去重 array(SELECT DISTINCT UNNEST regexp_split_to_array('1|2','|')::int[] )
7. 判断元素是否在数组中 SELECT 0 = ANY ('{1,2}'::int[]), SELECT 0 = ANY (lanes);
8. 分页大数据量的查询:
SELECT id FROM (SELECT id, row_number() over(ORDER BY id) AS rownum FROM ways WHERE tags IS NOT NULL) AS a
WHERE rownum%{page_size}=1 ORDER BY id
9. 十六进制字符串 转 整型:
DROP FUNCTION IF EXISTS hex_to_dec(in_hex TEXT);
CREATE FUNCTION
hex_to_dec(in_hex TEXT)
RETURNS BIGINT
IMMUTABLE STRICT LANGUAGE sql AS
$body$
SELECT CAST(CAST(('x' || CAST($1 AS text)) AS bit(16)) AS BIGINT);
$body$;
string_agg(field_name,separator) 把某列的值合并成字符串;
hstore函数:http://www.postgres.cn/docs/9.5/hstore.html
条件判断函数: https://blog.csdn.net/zhu4674548/article/details/55001210
http://www.zlovezl.cn/articles/15-advanced-postgresql-commands-with-examples/
数组操作:http://www.postgres.cn/docs/10/arrays.html#ARRAYS-SEARCHING
聚合函数:http://www.postgres.cn/docs/9.3/functions-aggregate.html