赞
踩
SELECT table_name FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = 'act' -- 数据库名称
AND table_name LIKE '%act_re%';
select column_name from information_schema.columns where table_name='ppom_object' ORDER BY ORDINAL_POSITION
select display_name as alias_display_name
select display_name as "显示名称"
with 临时表名1 as (sql查询), 临时表名2 as(sql查询)
select * from 临时表名1 t1, 临时表名2 t2
select * from (sql查询) temp where 条件
select uid,name, count(*) from table group by uid,name
select * from (
select row_number() over(partition by object_uid ORDER BY created_time ASC) AS new_index
)temp
where temp.new_index = 1
SELECT uid, array_to_string(array_agg(distinct display_name),';') AS name FROM tmp GROUP BY uid
case when 条件1 then 结果1
when 条件2 then 结果2
else 结果3
end 可对结果添加别名
select date_trunc('day',now()) = date_trunc('day',date('20230908'))
- 日期为date或timestamp,和字符串比较,字符串需要先转date或timestamp
select time1::timestamp > time2::timestamp
select time1::timestmap > to_timestamp('2023-11-15 18:33:41','yyyy-MM-dd hh24:mi:ss');
select to_date('2023-11-15 18:33:41','yyyy-MM-dd hh24:mi:ss');
select a > b
# 大于:> ,小于:< ,等于:= ,不等于 != 或 <>
select * from table where uid is null
select * from table where uid is not null
select * from table where uid = ''
select * from table where uid != '' 或 select * from table where uid <> ''
select * from table where length(uid) > 0
select * from table where length(uid) <= 0
select now();
select current_timestamp;
select to_char( now(),'yyyy-mm-dd hh:mi:ss');
select to_char(时间戳字段,'yyyy-mm-dd hh24:mi:ss');
#hh默认是12,可指定:hh12,hh24
select current_date;
select to_char( now() - interval '1 day','yyyy-mm-dd');
select to_char( now() - interval '1 week','yyyy-mm-dd hh:mi:ss');
select to_char( now() - interval '1 month','yyyy-mm-dd');
select to_char( now() - interval '1 year','yyyy-mm-dd');
select date_trunc('year', now())
select date_trunc('month', now())
select date_trunc('day', now())
select date_trunc('hour', now())
select date_trunc('minute', now())
select date_trunc('second', now())
select date_part('year', timestamp '2001-02-16 20:38:40')
#或者
select date_part('year', '2001-02-16 20:38:40'::timestamp)
select date_part('month', timestamp '2001-02-16 20:38:40')
select date_part('day', timestamp '2001-02-16 20:38:40')
select date_part('hour', timestamp '2001-02-16 20:38:40')
select date_part('minute', timestamp '2001-02-16 20:38:40')
select date_part('second', timestamp '2001-02-16 20:38:40')
select date_part('week', timestamp '2001-02-16 20:38:40')
// 从当前时间中提取年份 SELECT EXTRACT(YEAR FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取月份 SELECT EXTRACT(MONTH FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取天 SELECT EXTRACT(DAY FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取小时 SELECT EXTRACT(HOUR FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取分钟 SELECT EXTRACT(MINUTE FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取秒 SELECT EXTRACT(SECOND FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取世纪 SELECT EXTRACT(CENTURY FROM TIMESTAMP '2023-05-20 16:54:53.644833'); // 从当前时间中提取时间戳,单位:秒 SELECT EXTRACT(EPOCH FROM TIMESTAMP '2023-05-20 16:54:53.644833'); //取星期数 SELECT EXTRACT(DOW FROM TIMESTAMP '2023-05-20 16:54:53.644833');
#只获取年、月、日数据,其他忽略
select date_part('year', endtime::TIMESTAMP-starttime::TIMESTAMP)
select date_part('month', endtime::TIMESTAMP-starttime::TIMESTAMP)
select date_part('day', endtime::TIMESTAMP-starttime::TIMESTAMP)
#获取整体时间差,默认秒
#时间差换算成日
select date_part('epoch', endtime::timestamp - starttime::TIMESTAMP)/60/60/24
#可以外部套一层向下取整floor()、向上取整ceil()、四舍五入round()取整
select floor(date_part('epoch', endtime::timestamp - starttime::TIMESTAMP)/60/60/24)
#时间差换算成小时
select date_part('epoch', endtime::timestamp - starttime::TIMESTAMP)/60/60
#时间差换算成分钟
select date_part('epoch', endtime::timestamp - starttime::TIMESTAMP)/60
#只取日期部分计算
select Date(endtime) - DATE(starttime)
select postion('aa' in 'abcd')
select order_json::json->'orderBody' from order -- 对象域
select order_json::json->>'orderBody' from order -- 文本
select order_json::json#>'{orderBody}' from order -- 对象域
select order_json::json#>>'{orderBody}' from order -- 文本
# 列名:column,列值: {"key":"","name":"发起人修改","id":""}
select column::json ->> 'name'
#取数组长度
select json_array_length(name::json)
#取出json数组中的某个字段返回json数组
#列名:column,列值:[{"key":"","name":"xxx","id":""}]
select json_array_elements(column::json) -> 'name'
// a11b
select concat('a', 11, NULL, 'b');
// aabb
select 'aa' || 'bb'
-- 定义函数所处表,名称,public.f_daydelay, -- 输入参数名称 类型 -- 返回类型 RETURNS type -- 创建函数语言选择 LANGUAGE,可以选择(plpgsql,sql,plpython,plperl,...) -- 启动成本 COST 100(默认值) -- 波动率 VOLATILE (波动率默认分类),可以执行任何操作 CREATE OR REPLACE FUNCTION public.f_daydelay( starttime timestamp with time zone, endtime timestamp with time zone) RETURNS numeric LANGUAGE 'plpgsql' COST 100 VOLATILE PARALLEL UNSAFE AS $BODY$ --声明函数使用变量名称 类型,函数都需要使用分号结尾 DECLARE v_return varchar;--返回间隔时间 xx日xx时xx分 --函数主体 BEGIN --省略方法 --返回值 RETURN v_return; --异常处理 EXCEPTION WHEN OTHERS THEN RETURN SQLERRM; --主体结束 END; $BODY$; --定义函数授权用户 ALTER FUNCTION public.f_daydelay(timestamp with time zone, timestamp with time zone) OWNER TO postgres;
select f_daydelay('2023-05-24 11:38:14.38'::timestmap,'2023-06-20 11:38:14.38'::timestamp);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。