Oracle(PLSQL)入门学习二

上一篇:Oracle入门学习一

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

算术运算符:+ - * /

逻辑运算符:and or not

比较运算符:“=”、“<”、“>”、“<=”、“>=”、“<>”、“!=”。注意“=”是等于的意思而非赋值,最后两种都表示不等于。

查找列:

-- 给列起别名,如果列名有空格,则要用双引号包住
select name 名字,salary*15 "年    薪" from staff where name='张三';
--列的值如果要连接起来,使用||
select name||'-'||salary*15 "员工年薪" from staff where name='张三';
--常量列
select salary,'李子维' "姓名" from staff;
View Code

null运算:运算的时候如果有空值参与永远返回空,为避免此情况可以借助nvl(param1,param2)函数,param1为空则返回值为param2,否则param1。

select salary+nvl(bonus,0) all_salary from staff;

排重:无论多列还是单列,只需要在列的最前面加“distinct”关键字就可以实现排重,多列排重则是看整个组合是否重复,而非其中一列是否重复。

select distinct salary,name from staff;

where:条件筛选,可以加极限条件例如1=1永远为真,1!=1永远为假。字符串对比是区分大小写的。

-- 判空要用 is null
select salary,name from staff where salary>80000 or bonus is null;
-- 永真条件
select salary,name from staff where 1=1;
-- 永假条件
select salary,name from staff where 1!=1;
View Code
-- 字符串比较严格区分大小写,且用单引号括住
select salary,name from staff where name='Popo';
View Code

模糊查询之Like:通配符“_”表示任意一个字符,通配符“%”任意长度的字符串,使用“like”进行模糊查询经常用到这两个通配符。

select salary,name from staff where name like '张%';
select salary,name from staff where name like '张_';
select salary,name from staff where name like '%o_';

模糊查询之between...and...:等价于 “ 值a >= 值b  and 值a <= 值c”,切记包含等于。如果需要不在这个范围内的数据,只需在between前加个not关键字。

select salary,name from staff where salary between 80000 and 90000;
select salary,name from staff where salary>=80000 and salary<=90000;
select salary,name from staff where not salary between 80000 and 90000;

模糊查询之in:表示在某个范围内,但这个范围内的值都可以精确的表示,in(4,5,6)表示字段在4或5或6都可以。不在这个范围则在not in(......)。

select salary,name from staff where salary in (40000,80000);
select salary,name from staff where salary not in (40000,80000);

判null:用“is null”而非=null,而不空使用“is not null”。

-- 判断用is null,非空用 is not null
select salary,name from staff where bonus is null;
select salary,name from staff where bonus is not null;

order by 排序:排序语句永远放末尾,默认是 asc 升序(小->大),desc为降序(大->小),多列排序也只需写一次order by,例如“order by 列1 desc,order by 列2 asc,列3 desc”。

select salary,name from staff order by salary;
select salary,name from staff order by salary asc;
select salary,name from staff order by salary desc;
-- 不写降序升序,默认升序
select salary,name from staff order by salary ,name ;
select salary,name from staff order by salary desc,name desc;

 

posted @ 2020-05-01 21:57  舒碧  阅读(879)  评论(0编辑  收藏  举报