基本查询
列的计算:
select empno,ename ,sal ,sal*12 from emp;
SQL:
什么控制行?什么控制列?
什么控制列?select empno,ename from emp ;
什么控制行?where
字符串/字符 、日期 : 单引号
大小写问题:
a.命令/关键字:不敏感(不区别)
b.数据:敏感(区分)
运算符:
操作运算符: + - / * %
关系运算符: > >= < <= = !=或<>
如果是null,必须用is ,或is not
逻辑运算符: or and not
select* from emp where not (mgr = 7788 and job = 'CLERK')
select* from emp where mgr = 7788 and job = 'CLERK';
where执行顺序:右->左
null:
is/is not
null的计算:
任何数字 和null结算,结果为null
需要对null进行处理:null->0
nvl:if
nvl(comm,0 )
nvl2:if...else
nvl2(comm,comm,0)
if(comm==null) return 0
else return comm
select ename,comm,nvl(comm,0),nvl2(comm,comm,0) from emp;
对查询出的结果集去重:distinct
select distinct deptno from emp
连接符: java: "hello"+"world"->"helloworld"
oracle:
concat ||
dual:oracle提供的 学习时使用 临时表:单行单列
select 'hello'||'world' from dual;
修改oracle默认的日期格式
默认:DD-MON-RR
修改:
alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd' ;
alter session set NLS_DATE_FORMAT = 'DD-MON-RR' ;
---
范围查询: 数字/日期
between 小 and 大
>=小 and <=大
模糊查询:
like
配合通配符使用:_ :一个字符
% :任意个字符
ename like ..
数字、日期:like
姓名中第二个字母是M的员工信息:
select *from emp where ename like '_M%' ;
姓名中包含M的员工信息:
select *from emp where ename like '%M%' ;
姓名长度>6的员工信息: >6 >=7
select *from emp where ename like '_______%' ;
姓名中包含下划线的
zhang_san
select *from emp where ename like '%\_%' escape '\' ;
not in 不能出现null:如果出现了null,结果为null
select *from emp where deptno not in(10,20,30,null) ;
select *from emp where deptno not in(10,20,30,null) ;
select *from emp where deptno != 10 and deptno != 20 and deptno != 30 and deptno != null
select *from emp where mgr not in
(select mgr from emp);
排序:
order by 字段名|表达式|序号
select *from emp order by sal desc ;
select *from emp order by sal asc ;默认(升序)
select *from emp order by sal+10000 desc ;
null默认是最大值
多列排序 :
sal, hirdate
select *from emp order by sa desc,hiredate asc;
函数:
单行函数:一次操作一行
多行函数 :一次操作多行
单行函数: 字符函数 数值函数 日期函数 转换函数 通用函数
字符函数 : lower upper initcap(首字母大写)
dual :单行单列 ->单行
substr(str,begin,len) :从1开始数
length字符数 / lengthb 字节数
英文/数字
如果中文/符号:
utf-8编码格式下: 一个汉字/符号 占3个字节
gbk: 一个汉字/符号 占2个字节
查看当前系统编码格式:
select * from nls_database_parameters ;
insrt(str,substr)
select instr('helloworld','ll') from dual;
lpad/rpad:填充
select lpad('hello',10,'*') 左,rpad('hello',10,'*') 右 from dual;
trim:去掉任意字符
select trim('X' from 'XXXXXXXXXXXmyyXXXXXX') from dual;
replace
select replace('he**o','*','l') from dual;
数值函数:
round(数字,n位数):四舍五入 ,保留n位小数
select round(67.183,2), round(67.183,1),round(67.183,0),round(67.183,-1), round(67.183,-2)from dual;
trunc(数字,n位数):舍尾,保留n位小数
select trunc(67.183,2), trunc(67.183,1),trunc(67.183,0),trunc(67.183,-1), trunc(67.183,-2)from dual;
mod()求余
select mod(13,3) from dual;
日期:
sysdate:当前时间
格式化: 日期-》字符
to_char(日期,格式)
select to_char(sysdate,'yyyy-mm-dd') from dual;
日期+-数字 (默认是天)
select sysdate+1 from dual;
日期 - 日期
select ename, sysdate-hiredate from emp;
计算员工工龄 :入职日期 天 星期 月 年
select ename ,hiredate , (sysdate - hiredate) , (sysdate - hiredate)/7 , (sysdate - hiredate)/30, (sysdate - hiredate)/365 from emp;
months_between(日期1,日期2) : 日期1-日期2
add_months(日期,月数):
当前最大是第几天 last_day
下一个星期n是哪一天next_day
select next_day(sysdate,'星期五') from dual ;
round
trunc
if
else if
else if.
...
else
通用函数 :
a. nvl/nvl2
b. nullif(a,b) :a=b,null ,否则返回a
c coalesce :从左往后 找到第一个不为null的值
select ename, sysdate-hiredate from emp;
d.条件判断函数
decode(字段,条件1,返回值1,条件2,返回2,....,最后表达式)
select ename,job , sal 涨前, decode(job, 'PRESIDENT',sal+1000,'MANAGER',sal+500,sal+300) 涨后 FROM EMP;
case表达式
select ename ,job ,sal 涨前,case job
when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+500
else sal + 300 end
涨后
from emp ;
case
when ... then
when ... then
else
end ;
转换函数:
隐式转换/显式转换
隐式转换:
字符-数字
数字-字符
字符-日期
日期-字符
显式转换:
字符->数字
select to_number('¥123,456.7', 'L999,999.9') from dual ;
数字->字符
select to_char(123456.7,'L999,999.9') from dual;
字符-日期
日期-字符
to_char
多行函数:组函数、 聚合函数
count(*) -> 19
求有几个部门
count() 自动排除null
max/min/avg/sum
分组:
各个部门总工资: 对部门分组 dept 10 20 30 40
select deptno,avg(sal) from emp 【group by deptno】 ;
select deptno , avg(sal) from emp group by deptno ;
10 8000
20 6000
30 5000
40 9000
分组查询时,不在组函数(多行函数)中的列,必须在group by中。
先根据部门分组 ,再根据job分组
--各个部门中各个工作 的平均工资
select deptno,job,avg(sal) from emp group by deptno ,job ;
对行筛选用where
对组进行筛选用Having
select deptno,job,avg(sal) from emp group by deptno,job having avg(sal)>500
可以在Having使用多行函数count min avg
但是 不能在where中使用多行函数

浙公网安备 33010602011771号