Oracle数据常用操作
Posted dream_sky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle数据常用操作相关的知识,希望对你有一定的参考价值。
将用逗号隔开字段拆分成两行:
select * from mp_fs_file_info a,dm_process_upload b where instr(b.attachment,a.file_id,1,1)>0;
字段名小写加双引号
to_char转换成制定格式。
select to_char(xmmc), 项目名称,dwmc "name" from desulfidation,
插入多条数据:
insert into imdt_role_permission (
(select 3,5 from dual)
union
(select 3,23 from dual)
union
(select 3,30 from dual)
union
(select 3,31 from dual)
)
)
delete from calm_role_permission where role_id=2 and permission_id between 1 and 12
查询列表最大数据记录方法:
select a.* from pc_evaluation_upload a where not exists(
select 1 from pc_evaluation_upload b where b.well_id=a.well_id and b.create_date >a.create_date) and a.well_id =‘0000000039‘;//两个字段不出现重复,时间相同时需要用distinct
?
?
select a.* from pc_evaluation_upload a inner join
(select well_id,max(create_date) maxgdtime from pc_evaluation_upload group by well_id) b
on a.well_id=b.well_id and a.create_date=b.maxgdtime
你可以理解为 JOIN 是 INNER JOIN 的缩写。
RIGHT JOIN 等价于 RIGHT OUTER JOIN,等价于from t1,t1 where t1.id =t2.id
having:
group by以后的where过滤。
1
2
3
|
查询字段出现两次以上的数据,也可用于在另一张表中 SELECT * FROM material_plan_ming a WHERE a.material_plan_id IN ( SELECT b.material_plan_id FROM material_plan_ming b GROUP BY b.material_plan_id HAVING COUNT (b.material_plan_id)>1); |
distinct和group by去重区别:
即只有所有指定的列信息都相同,才会被认为是重复的信息。
单纯的去重操作使用distinct,速度是快于group by的。
group by使用的频率相对较高,但正如其功能一样,它的目的是用来进行聚合统计的,虽然也可能实现去重的功能,但这并不是它的长项
group by 的优势是可以包含一个聚合函数,最大值,max,min,sum等。
不包含聚集函数的GROUP BY操作来说,和DISTINCT操作是等价的
1
2
3
4
5
6
7
|
SELECT DISTINCT a.key_id a.well_id,a.barrier_type_id FROM pc_evaluate_wellhead a WHERE a.well_id= ‘0000000206‘ select e.well_id,e.barrier_type_id from pc_evaluate_wellhead e group by e.well_id,e.barrier_type_id SELECT DISTINCT well_id FROM pc_evaluate_wellhead; SELECT well_id, COUNT (well_id) from pc_evaluate_wellhead group BY well_id; |
行转列,group分组查询,逗号隔开:
select con_basis_id, sum(con_amount) from contract_basis_pro group by con_basis_id;
select LISTAGG(to_char(pro_name),‘,‘) WITHIN GROUP( ORDER BY con_basis_id) pro_name,con_basis_id from contract_basis_pro group by con_basis_id;
select con_basis_id,to_char(wm_concat(pro_name)) pro_name,to_char(wm_concat(con_amount)) con_amount,to_char(wm_concat(remark)) remark
from contract_basis_pro group by con_basis_id ;
select con_basis_id,wm_concat(to_char(pro_name)) pro_name,wm_concat(to_char(con_amount)) con_amount,wm_concat(to_char(remark)) remark from contract_basis_pro group by con_basis_id ;
wm_concat(toto)over(partition by ename order by type) |
行转列排序问题:
1
2
3
|
SELECT con_basis_id, max(pro_id) pro_id FROM(SELECT con_basis_id, to_char(wm_concat(to_char(pro_id)) over (partition by con_basis_id order by ID)) pro_id FROM contract_basis_pro ) GROUP BY con_basis_id |
列转行:
select REGEXP_SUBSTR(a.file_url ,‘[^,]+‘,1,l) as file_url
from reply_file a,
(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=100) b
WHERE l <=LENGTH(a.file_url) - LENGTH(REPLACE(file_url,‘,‘))+1;
查询有重复的语句:(having 对group后的组进行过滤,筛选。某些条件可用where替换)
select * from pro_bas_info where pro_code in (select pro_code from pro_bas_info group by pro_code having COUNT(*)>1);
select well_id, max(create_date) maxgdtime from pc_evaluation_upload group by well_id HAVING count(*) > 2
分页查询:(第二种方式效率较高)
SELECT a.*, ROWNUM RN FROM (SELECT * FROM mp_fs_file_info ORDER BY R_CREATEDATE DESC) a
WHERE ROWNUM BETWEEN 0 AND 4;
SELECT * FROM (
SELECT a.*, ROWNUM RN FROM (SELECT * FROM mp_fs_file_info ORDER BY R_CREATEDATE DESC) a
WHERE ROWNUM <=4) b
WHERE RN > 0;
IN和EXISTS的用法和区别:
exists : 强调的是是否返回结果集,不要求知道返回什么
in 最大的区别在于 in引导的子句只能返回一个字段
select a.* from contract_basis a where exists(select 1 from signed_file b where a.id = b.con_basis_id); //查询在另一张表中有数据,有附件。
select a.* from contract_basis a where a.id in (select b.con_basis_id from signed_file b where a.id = b.con_basis_id);
DELETE FROM contract_basis_pro t1 WHERE NOT EXISTS (SELECT 1 FROM pro_bas_info t2 WHERE t1.pro_id = t2.pro_code); 查询在另一张中不存在的段。
按排序取出第一条数据:
with ti as (SELECT ID,MK_NO FROM (SELECT ID,MK_NO FROM MP_APP_DEPT WHERE PID = ‘JGZNBM‘)
WHERE mk_no>‘020202‘ order by mk_no)
select * from ti where rownum =1;
按排序取出第一条数据:
SELECT a.key_id,b.file_id FROM dm_process_upload a,mp_fs_file_info b WHERE a.attachment = b.file_id(+) ;
select to_char(sysdate,‘YYYY-MM-DD HH24:MI:SS‘) d1 from dual;
to_date(‘2010-02-24 15:01:54‘,‘YYYY-MM-DD HH24:MI:SS‘)
select sysdate d1,trunc(sysdate) d2 from dual;//只要日期部分
select sysdate d1,EXTRACT(YEAR FROM sysdate) thisyear//日期的部分YEAR、MONTH,DAY、HOUR、MINUTE、SECOND
select date‘2017-12-29‘-a.branch_declare_date from special_evaluation a;
正则表达式:
SELECT * FROM pay_info WHERE 1=1 ORDER BY to_number(regexp_replace(doc_no, ‘[^0-9]‘)) DESC;//to_number的用法。
SELECT regexp_replace(‘20322323-07‘, ‘^[0-9]‘) FROM dual;
select regexp_replace(‘XX路1001弄12幢102室‘, ‘d+幢‘, ‘‘) from dual 可取消第三个参数
sql字段为空则替换:
SELECT t.task_id,t.process_id,to_number(nvl(t.task_id,0))+to_number(nvl(t.process_id,0)) FROM material_plan t;
replace与translate都是替代函数:
select translate(‘abcdcefga‘,‘abc‘,‘wo‘) 返回值 from dual;//wodefgw
SELECT Replace(‘abcdcefga‘,‘abc‘,‘wo‘) 返回值 FROM dual;//wodcefga
只不过replace针对的是字符串,而translate针对的是单个字符,没有出现的,如‘c’也删除。
Union和Union All的区别:
1、Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
2、Union All,对两个结果集进行并集操作,包括重复行,不进行排序;3、都是取select后的字段名作为统一字段名,union后的字段按顺序组合即可。
清除字段中有空格的字段:
select REPLACE(report_no, ‘ ‘, ‘‘) from contract_basis;
update contract_basis set report_no=REPLACE(report_no, ‘ ‘, ‘‘);
查询时间fan范围:
sysdate+1就是加一天
如果要加一小时的话就是sysdate+1/24
如果要加一分钟的话就是sysdate+1/24/60
--数据恢复:(delete闪回,drop回收站)
select * from user_recyclebin;--查询回收站内容
flashback table 要恢复的表名 to before drop;--恢复表
flashback table 表A to before drop rename to 表B;--从回收站恢复时重命名表(把表A重新恢复命名为表B)
1、 insert into 表名 (select * from 表名 as of timestamp to_timestamp(‘删除时间点‘,‘yyyy-mm-dd hh24:mi:ss‘));-- 注意要保证主键不重复。
2、
--开启行移动功能
·alter table 表名 enable row movement
--恢复表数据
·flashback table 表名 to timestamp to_timestamp(删除时间点‘,‘yyyy-mm-dd hh24:mi:ss‘)
--关闭行移动功能 ( 千万别忘记 )
·alter table 表名 disable row movement
以上是关于Oracle数据常用操作的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Toad for Oracle 中使用自定义代码片段?
Oracle数据库从RMAN备份集片段还原指定单个归档日志进行日志挖掘分析
Oracle 数据库 - 使用UEStudio修改dmp文件版本号,解决imp命令恢复的数据库与dmp本地文件版本号不匹配导致的导入失败问题,“ORACLE error 12547”问题处理(代码片段