一.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查询某段时间之前的数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论