Oracle(PLSQL)入门学习三

上一篇:Oracle入门学习二

学习视频:https://www.bilibili.com/video/BV1tJ411r7EC?p=26

字符串函数:length、upper、lower、initcap、 concat、instr、replace。

-- dual 常量表,没什么意义,就语法规则
-- 获取字符串长度
select length('我是谁?') from dual;
select length('abcd') from dual;
-- 全部变成大写
select upper('abcdDDDFFF') from dual;
-- 全部变成小写
select lower('DDAFAFA') from dual;
-- 首字母大写化,后面的也会变成小写
select initcap('abcdDDD') from dual;
-- 从第一个字符开始截取三个字符
select substr('123456789',1,3) from dual;
-- 从第三个字符开始截取三个字符
select substr('123456789',3,3) from dual;
--字符串连接函数
select concat('ab','dc') from dual;
--替换函数
select replace('我恨你','','') from dual;
--查找字符串出现的首位置
select instr('java','va') from dual;

数字函数:round、trunc。

--四舍五入的取小数位
select round(3.14159,3) from dual;
select round(3.14159,2) from dual;
--截取小数位,但没有四舍五入
select trunc(3.14159,3) from dual;

转换函数:to_char、to_date、to_number

--数字转字符串
--L指本地货币
--$指美元
select to_char(5006,'L9999.99') from dual;
select to_char(5006,'$9999.00') from dual;
select to_char(5006.989,'9999.00') from dual;
--字符串转日期
select to_date('2019-05-12','yyyy-mm-dd') from dual;
select to_date('2019/05/12','yyyy/mm/dd') from dual;
select to_date('2019/05/12 12:23:23','yyyy/mm/dd hh:mi:ss') from dual;
--字符串转数字 
select to_number('500')+800 from dual;

plsql基本语法熟悉之后,可以配置一下快捷键提高效率:https://jingyan.baidu.com/article/215817f7e1efbb1eda1423ef.html

聚合函数:处理多个数据的函数,常见的有max、min、count、sum、avg。

--列有空也不影响
select max(salary) from staff;
select min(salary) from staff;
select max(bonus) from staff;
select sum(bonus) from staff;
select avg(bonus) from staff;
--count(某列),当该列值不为空才列入计算
select count(bonus) from staff;
--总行数
select count(*) from staff;

分组函数:group by,根据一列或多列分组,使用聚合函数同级该组的数据。

where用来筛选from字句产生的行,group by用来分组where字句之后的数据,having用来过滤分组之后的数据。

--单列分组 
select name,count(*) from staff group by name;
--多列分组
select department,salary,count(*) from staff 
group by department,salary order by department desc;
--对分组之后的数据,再次进行条件过滤,使用having关键字而非where
select department,salary,count(*) from staff 
group by department,salary 
having count(*)<8 order by department desc ;
--对分组之后的数据,再次进行条件过滤,使用having关键字而非where
select department,salary,count(*) from staff 
where salary>40000
group by department,salary 
having count(*)<8 order by department desc ;

 子查询:查询套查询。执行顺序先子后父。

select *
  from student
 where born_date >
       (select born_date from student where student_name = 'bibi')

 

posted @ 2020-05-02 16:31  舒碧  阅读(201)  评论(0编辑  收藏