常用的技巧,再次记录保存
1、如何给类型为varchar2类型的id字段转换为number类型并增加值,不要说要用序列实现。
insert into test values((select to_char(max(to_number(id))+1) from test),'test','1','1','1')
2、查询某个字段有多少条不重复数据
如表testTble,字段studentId
select count(distinct studentId) from testTable
3、根据指定字段,查询哪些数据有多条记录,重复记录
3.1列出有多条记录,列出查询字段,可用于查询重复记录,并返回单条记录
select c_type from table_test m having (select count(c_type) from table_test c where c.c_type =m.c_type) >1 group by m.c_type
3.2某个字段A的某个值XX,对应字段B有多条数据。仅查询这些数据,可用于查询重复的记录,并返回多条记录
select * from Test where A in(select A from Test group by A having count(distinct B)>1) order by A
4、复制表:
带原表记录一起复制:create table mewTable as select * from oldTble
只复制表结构:create table mewTable as select * from oldTble where 1=2
5、通过pl/sql向oracle数据库中插入数据时,如值中有特殊字符&,数据库会认为和&连接的后面的内容是一个变量,然后弹出下图所示窗口。如插入的值是C&C,那么变量就是&C。需要注意的是这个“&CBA=33”里面有“=”号,此种情况是,如果&后面遇到了"+","-","*","/","%"等运算符,那么这些运算符作为结束。所以如果插入的值是&CBA=33,那么变量值是&CBA。
那么我们实际上要把"&"作为特殊字符插入到数据库中,该如何处理呢?有两种方法:
第一种:使用丘比特的虾米365博友的方法,参考链接:https://blog.youkuaiyun.com/u011109356/article/details/77677577
insert into tab1 values('aaa','bbb','c&c');
第二种方法:使用ASCII转码,查询下表,&对应为CHR(38), %对应的是CHR(37)等等。
所以执行:insert into tab1 values('aaa','bbb','c'||CHR(38)||c'); 即可。
你觉得那种方法更好呢?欢迎各路大侠留言评论。
6、一道关于group by的面试题:https://blog.youkuaiyun.com/Eric_YS/article/details/103363955
7、oracle分页查询:
Select *
From (Select RowNum As LineNum, T.*
From (select *
from table1
where 1 = 1
and proposalno like '%123123%' --可替换参数
order by 字段1, 字段2) T
where RowNum <= 1)--可替换参数
where LineNum > 0--可替换参数