简单总结下
删除数据量大的表
truncate table A;
相比delete 效率更高,因为没有走事务,但是不能回滚
minus用法
SQL1 minus SQL2
SQL1中有的,SQL2中没有,会显示出来,SQL2中有的,SQL1中没有,不会显示出来
嵌套子查询中不能有orde by
select * from A where A.id in (select id from B order by name);--类似这种写法;错误
order by子句只能对最终查询结果排序
但是这样写又可以,下面这个写法叫内联视图
select * from A where A.id in (select * from (select id from B order by name));
这样也可以
select * from (select info,RANK() OVER(PARTITION BY info ORDER BY times DESC) RANK
FROM tmp_S_CUST_SIGNINFO);
to_char,to_date的用法
select to_date(substr(start_time,1,10),'yyyy-mm-dd') from A
select to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd') from dual;
递归查询
select * from sys_org
start with orgtype = '0'
connect by prior orgid = parentorgid;
查出来类似树形,具体啥样就不演示了
with as 用法
with table as (select * from ...);
相当于建了个临时表,只能查询,不能更新。简化写法,也可以提高效率。
dual表的用法
属于SYS schema的一个表,然后以PUBLIC SYNONYM的方式供其他数据库用户使用。
序列经常用
select aaa.nextval from dual;
select 1+2 from A;
......
可以update,insert,delete,drop 。drop dual数据库会出问题,不过也是可以恢复的。
sum函数出现精度丢失的问题
select 9999999999999999.91 from dual;
select cast(9999999999999999.91 as decimal(18,2)) from dual;
select to_char(0.01) from dual 数据库小于1的数字,用char接受,会出现0丢失的情况,, 如 0.09 变成 .09 非常的恐怖
oracle分组后去第一条,此sql屡试不爽,推荐使用
SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY x ORDER BY y DESC) rn, test1.* FROM test1) WHERE rn = 1 ;