ORACLE常用SQL
查看表结构
Oracle 中用sql语句查看外键约束和表结构_oracle查看表结构的sql语句_何以解忧,唯有..的博客-CSDN博客
查看占用/行数最多的表
select owner,table_name,num_rows,blocks*8/1024/1024 MB from dba_tables where num_rows is not null order by num_rows desc
日期/时间
TO_DATE('1970-1-1','YYYY-MM-DD')+(g.gather_time/(3600*24))
模糊匹配
like 模糊查询
这就要用的like模糊查询了,其通配符如下:
% 表示零个或多个字符
(1)_ 单一任意字符
(2)\ 特殊字符
[ ] 在某种范围内的字符,如[0-9]、[abcd]
[^ ] 不在某种范围内的字符,如[0-9]、[abcd]
其中条件模糊查询,Oracle数据库中常用的有下面两种方式:
1、% 表示任意零个或多个字符。可匹配任意类型和长度的字符,一般使用两个百分号(%%)表示
如下示例:把其中带有S的名字全部查询了出来。
2、_ 表示任意单个字符。匹配单个任意字符,常用来限制要查询的字符长度
一列值复制到另一列
update tab_gatheross2_base a set a.X_CU_SERIALNUMBER=(select b.SERIALNUMBER from tab_gatheross2_base b where b.device_id=a.device_id) where a.X_CU_SerialNumber is null or a.X_CU_SerialNumber ='EMPTY' or a.X_CU_SERIALNUMBER like '%:%';
选取多条记录中某列值最大的记录
select tab_bind a ,(select max(binddate) binddate, device_id from tab_bind group by device_id) b where a.device_id=b.device_id and a.binddate=b.binddate
分析函数row_number(分组多条记录+筛选+选择某条记录、排序)
SQL中row_number() over(partition by)的用法说明_Mysql_脚本之家 (jb51.net)
s_id 表是学生编号,c_id表是课程编号,s_score 表是学生对应的课程分数
1.要求:得出每门课程的学生成绩排序(升序)
select * ,row_number() over (partition by c_id order by s_score) from score;

2:进一步要求:得出每门课程的学生成绩,并且按照70分作为分割线排序—即低于70分的排序,高于70分的排序
select * ,row_number() over (partition by c_id,(case when s_score>70 then 1 else 0 end) order by s_score) from score;

3.进一步要求:如果只需查询出不重复的姓名即可,则可使用如下的语句, 由查询结果可知,姓名相同年龄小的数据被过滤掉了;
SELECT * FROM ( SELECT name ,age, detail ,ROW_NUMBER() OVER ( PARTITION BY name ORDER BY age DESC )RN FROM TEST_Y ) aaa WHERE aaa.RN= 1 ;

4.分页
--先做一个子查询,先按id1进行排序,排序完后,给每条记录进行了编号
--然后再将子查询做为一张表,就可以进行分页了
select *
from (select t.*,row_number() over(order by t.id1 asc) as rn from demo t) d
where d.rn between 1 and 2
查看非数字的记录
select trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789', '#'))
from TestChar
where trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789', '#')) is not null;

浙公网安备 33010602011771号