当前位置: 代码网 > it编程>数据库>Oracle > oracle查询一天前、几天前、几小时前、一小时前的数据以及恢复误删的数据

oracle查询一天前、几天前、几小时前、一小时前的数据以及恢复误删的数据

2024年05月15日 Oracle 我要评论
一.oracle查询一天前、几天前、几小时前、一小时前的数据语法:select * from <table_name> as of timestamp (sysdate-<inter

一.oracle查询一天前、几天前、几小时前、一小时前的数据

语法:

select * from <table_name> as of timestamp (sysdate-<interval>);

例:

1.您想查询用户表(user_info)一小时前的数据

--一小时是一天的1/24 ,故<interval>参数为1/24,如果想查四小时前的数据,<interval>参数可以是4/24或1/6
select * from user_info as of timestamp (sysdate-1/24);

2.您想查询用户表(user_info)一天前的数据

select * from user_info as of timestamp (sysdate-1);

3…您想查询用户表(user_info)三天前的数据

select * from user_info as of timestamp (sysdate-3);

二.恢复误删的数据

恢复误删的数据基于前面能否查询到历史数据,假设能够查到一天前的数据,就可通过下面语句恢复被删除的数据 (如果您觉得步骤不直观的话请您直接看例子吧)

步骤:

1.创建临时表并把误删的数据暂时存到临时表中

create table tabname_temp as select * from tabname as of timestamp (sysdate-<interval>);

2.把临时表的数据插入原表(被您误删数据的表)中

 -- select * from tabname_temp 语句 可以带where条件,看您需要插入所有数据还是选择性插入
 insert into tabname select * from tabname_temp ;

3.删除临时表 (防止临时表占用数据库空间)

drop table tabname_temp;--注意这里删除的是临时表,您刚刚创建的表,别删错了。

例:恢复一天前误删除的用户(用户 “tom”,“jerry”)

create table user_info_temp as select * from user_info as of timestamp (sysdate-1);
insert into user_info select * from user_info_temp where username in ('tom','jerry');
drop table user_info_temp;

需要注意的是:误删的数据不一定能查询到,也不一定能找回,故在操作数据库时尽量小心操作

例:在更新数据库表时一定带上where条件;删除数据之前先备份数据(尤其是生产环境或测试环境您不熟悉的表)

附:oracle 查询某个时间之内的数据

//一月之内
select * from ess.e_log where e_log."etime" between sysdate-interval '7' month and sysdate;
 
select * from ess.e_log
where to_char(e_log."etime",'yyyy-mm-dd hh24:mi:ss') between to_char(sysdate-225,'yyyy-mm-dd hh24:mi:ss') and to_char(sysdate,'yyyy-mm-dd hh24:mi:ss');
 
//一周之内
select * from ess.e_log where e_log."etime" > sysdate - interval '7' day;
 
 
//一年之内
select count(*) from ess.e_log where e_log."etime" between sysdate-interval '1' year and sysdate;
 
//sysdate获取的时间是oracle数据库系统时间
select sysdate from dual;

总结

到此这篇关于oracle查询一天前、几天前、几小时前、一小时前的数据以及恢复误删的数据的文章就介绍到这了,更多相关oracle查询某段时间之前的数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com