0.sql的执行顺序
from .. where .. group by .. having .. select .. rownum .. order by ..
1.select的区别:
select 1+1; 此句在mysql中可以执行,但是在Oracle数据库中不可以使用,
Oracle必须要补全表名,select 1+1 from dual;(dual是为了补全语法的系统的一个虚表)
2.别名查询:
别名查询使用as关键字,可以省略
select ename 姓名,sal 工资 from emp;
别名中不能使用特殊字符或关键字,如果有的话就加双引号
select ename "姓 名",sal 工资 from emp;
3.去除重复数据:distinct
去除单列重复数据
select distinct job from emp;
去除多列重复数据(每一列都相同才算重复)
select distinct job, deptno from emp;
4.空值问题
null值,代表不确定的、不可预知的内容,不可以做四则运算
nvl函数:如果参数1为null,就返回参数2
select sal*12 + nvl(comm,0) from emp;
5.字符串拼接
(在oracle中双引号用于表示别名,单引号表示拼接)
oracle特有的字符串拼接符:||
select '姓名'||ename from emp;
函数拼接:concat(str1,str2)
select concat('姓名',ename) from emp;
6.转义字符
使用escape表示转义字符 escape 后面接表示转义字符的符号
(查询姓名中包含%的员工信息)
select * from emp where ename like '%/%%' escape '/';
7.排序
升序 asc (默认)
降序 desc
排序时注意Null问题,默认Null数据排在前面nulls first,使用nulls last 可以使null值排在后面
select * from emp order by comm desc nulls last;
8.数值函数
floor(value) 向下取整
ceil(value) 向下取整
round(value,[num]) 四舍五入, num 为精确度
round(45.926,2) --45.93
round(45.926,1) --45.9
round(45.926,0) --46
round(45.926,-1) --50
round(45.926,-2) --0
round(65.926,-2) --100
trunc(value,[num]) 截断,num为精确度
trunc(45.926,2) --45.92
trunc(45.926,1) --45.9
trunc(45.926,0) --45
trunc(45.926,-1) --40
trunc(45.926,-2) --0
trunc(65.926,-2) --0
mod(value1,value2) 取余函数
9.通用函数
nvl(参数1,参数2) 如果参数1=null,就返回参数2
nvl2(参数1,参数2,参数3) 如果参数1=null,就返回参数3,否则返回参数2
nullif(参数1,参数2) 如果参数1=参数2,就返回null,否则返回参数1
coalesce(参数1...) 返回第一个不为null的值
10.条件表达式
mysql和oracle通用
case 字段
when 值1 then 值
when 值2 then 值
when 值3 then 值
else
默认值
end
oracle特有
decode(字段,if1,then1,if2,then2,eles1)
11.exists(查询语句)
存在的意思,当作布尔值来处理
当查询语句有结果时,返回true
当查询语句没有结果时,返回false
12.分页查询
在Oracle中没有分页查询,只能借助子查询来实现分页查询
查询第6到第10条记录
select * from (select rownum hanghao,emp.* from emp) tt where tt.hanghao between 6 and 10;
13.并集运算
union:去除重复的,并且进行排序
union all:不会去除重复的
列的类型要一致,按照顺序写,结果数量要一致(如果不足用null填充,或者写相同类型的数据填充)
14.差集运算 minus