第一部分地址:https://blog.csdn.net/scut_yfli/article/details/104786477
六、格式转换
-
--TO_NUMBER
-
select to_number(
'123')
from dual;
--123
-
-
--TO_DATE
-
select *
from table1
where birthdate =
to_date(
'1980年12月17日',
'yyyy"年"mm"月"dd"日"');
-
select *
from table1
where hiredate =
to_date(
'1980-12-17',
'yyyy-mm-dd');
-
-
--TO_CHAR
-
select to_char(
sysdate,
'yyyy "年" mm "月" dd "日" day')
from dual;
--2020年 03 月 10 日 星期二
-
select to_char(
1234,
'$9,999')
from dual;
--$1,234
七、字符处理函数
-
--ASCII码转换为字符
-
select
chr(
65)
from dual;
--A
-
-
--字符转换为ASCII码
-
select
ascii(
'A')
from dual;
--65
八、数据判断处理
-
--处理Null值
-
select nvl(
null,
10)
value
from dual;
--10
-
select nvl(score,
10) score
from student;
--score不为空时返回10
-
select nvl2(
null,
10,
20)
value
from dual;
--20
-
select nvl2(score,
10,
20) score
from student;
--score不为空时返回10
-
-
--decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
-
select
decode(
sign(变量
1-变量
2),
-1,变量
1,变量
2)
from dual;
--实现取变量1、变量2中的较小值
-
-
--使用case分类
-
select
case
when score
between
0
and
60
then
'F'
-
when score
between
60
and
80
then
'C'
-
when score
between
80
and
90
then
'B'
-
when score
between
90
and
100
then
'A'
-
else
'NULL'
-
end
-
from table1;
九、表连接
关于各种连接的解释,参考文章:https://blog.csdn.net/scut_yfli/article/details/104665248
-
--左外部连接:
-
select *
from table1
left
join table2
on table1.key = table2.key
-
select *
from table1,table2
where table1.key = table2.key(+)
-
-
--右外部连接
-
select *
from table1
right
join table2
on table1.key = table2.key
-
select *
from table1,table2
where table1.key(+) = table2.key
-
-
--完全外部连接
-
select *
from table1
full
outer
join table2
on table1.key = table2.key
-
-
--内部连接
-
select *
from table1
inner
join table2
on table1.key = table2.key
-
-
--左反连接
-
select *
from table1
left
join table2
on table1.key = table2.key
where table2.key
is
null
-
-
--右反连接
-
select *
from table1
right
join table2
on table1.key = table2.key
where table1.key
is
null
-
-
--全反连接
-
select *
from table1
full
outer
join table2
on table1.key = table2.key
where table1.key
is
null
or table2.key
is
null
十、集合操作
-
--并集:将查询的返回组合成一个结果, union all不过滤重复
-
select *
from table1
where
id <
4
-
union
-
select *
from table1
where
id >
2
and
id <
6
-
-
--交集:返回查询结果中相同的部分
-
select *
from table1
where
id <
4
-
intersect
-
select *
from table1
where
id >
2
and
id <
6
-
-
--差集:返回在第一个查询结果中与第二个查询结果不相同的那部分行记录
-
select *
from table1
where
id <
4
-
minus
-
select *
from table1
where
id >
2
and
id <
6
十一、性能相关
-
--表压缩
-
alter
table table1
move
compress;
--压缩
-
alter
table table1
move
nocompress;
--解压
-
-
--取消表日志
-
alter
table table1 nologging;
-
alter
table table1
logging;
-
-
--改变优化器模式
-
select
/*+ rule */ *
from table1
where
id =
100;
十二、函数及过程
-
--函数:编写一个函数计算学生某一门课程在班级内的排名
-
create
or
replace
function sf_score_pm(
-
p_in_stuid
in varchar2,
--学号
-
p_in_courseid
in varchar2
--课程ID
-
)
-
return
number
-
is
-
ls_pm
number:=
0;
-
ls_score number:=0;
-
begin
-
--获取该学生的成绩
-
select t.score
into ls_score
from score t
-
where t.stuid = p_in_stuid
-
and t.courseid = p_in_courseid;
-
--获取成绩比该学生高的人数
-
select
count(
1)
into ls_pm
from score t
-
where t.courseid = p_in_courseid
-
and t.score>ls_score;
-
--得到该学生的成绩排名
-
ls_pm:=ls_pm+1;
-
return ls_pm;
-
exception
-
when no_data_found then
-
dbms_output.put_line('该学生的课程:'||p_in_courseid|| '的成绩在成绩表中找不到');
-
end;
-
-
--过程:与函数类似,但没有返回值
-
create
or
replace
procedure sf_score_pm(
-
p_in_stuid
in varchar2,
--学号
-
p_in_courseid
in varchar2
--课程ID
-
)
-
as
-
begin
-
--......
-
end;
转载:https://blog.csdn.net/scut_yfli/article/details/104787772
查看评论