查询和单行函数
修改oracle默认的日期格式
默认:DD-MON-RR
修改:
alter session set NLS_DATA_FORMAT = 'yyyy-mm-dd';
alter session set NLS_DATE_FORMAT = 'DD-MON-RR';
范围查询:
between 小 and 大
- select table_name from user_tables;
- < > =
模糊查询:
- like
- 使用通配符进行查询: __ :一个字符; %:任意个字符
- ename :like;
- 数字、日期 :like……
- 查询姓名中包含下划线的:select * from emp where ename like '% \ _%' escape ' \ ';
- not in 不能出现 null 如果有 则结果为空
```
```sql
排序:order by 字段名|表达式|序号|
- asc 升序(默认), desc 降序
- select * from emp order by sal desc;
- select * from emp order by 1 asc;
- select empno,ename.sal from emp order by 3 asc;
- 排序的时候 null 默认时最大值
- 把bull放在最后面 + nulls last;
- 追加排序:
- select empno,ename,sal from emp;
- a order by sal desc;
- /
- 多列排序:
- select * from emp order by sal desc,hiredate asc;
```
函数:
单行函数(一次操作一行):字符函数 数值函数 日期函数 转换函数 通用函数
- - - ```sql
字符函数
- lower : 转小写
- upper : 转大写
- initcap:单词开头大写
- dual:单行单列-->单行
- substr(str,begin,len):从1开始数
- length:字符
- lengthb:字节
- 英文/数字 字符/字节相等
- 中文/符号:UTF-8一个汉字/符号占三个字节
- 查看当前系统的编码格式
- select * from nls_database_parameters;
- insrt(str,substr):在str中找‘substr’的位置
- 填充:
- 左填充:lpad('hello',10,'*')
- 右填充:rpad('hello',10,'*')
- 10 表示总位数,* 表示需要填充的标志
- trim:默认去除空格
- select trim ('X' from 'XXXXXXHello WorldXXXXXXX');
- X 表示需要去除的符号;
- replace : 替换
- select replace ('hello','l','*') from dual;
```
```sql
数值函数
- round(数字,n位数):四舍五入
- select round(67.183,2)一, round(67.183,1)二, round(67.183,0)三,round(67.183,-1)四,round(67.183,-2)五 from dual;
- 保留n位小数
- trunc(数字,n位数)
- 舍尾
- mod():求余数
```
```sql
日期函数:
- sysdate:当前时间
- select sysdate from dual;
- 格式化:
- 日期 --> 字符串
- 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
- select ename , months_between(sysdate,hiredate) from emp;
- 日期比较精确
- add_months(日期,月数):加月数
- select add_months(sysdate,12) from dual;
- 当月最大是第几天 last_day
- select last_day(sysdate) from dual;
- 下一个星期n是那一天 next_day(日期,星期n)
- select next_day(sysdate,'星期五') from dual;
- '星期n' 不能写成 '周n'
- round
- select round(sysdate,'month'),round(sysdate,'year')from dual;
-
- trunc
- select trunc(sysdate,'month'),trunc(sysdate,'year')from dual;
```
- - - ```sql
通用函数
- nvl(表达式1,表达式2)
- 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值
- 该函数的目的是把一个空值(null)转换成一个实际的值
- NVL2(表达式1,表达式2,表达式3)
- 如果表达式1为空,返回值为表达式3的值。如果表达式1不为空,返回值为表达式2的值。
- nullif(a,b) : a=b,null,否则返回a
- select nullif('abc','abc')from dual;。
- select nullif('abc','aaa')from dual;
- coalesce : 从左往右,找到第一个不为null的函数
- select comm,sal , coalesce(comm,sal) from emp;
```
```sql
- 条件判断函数
- decode(字段,条件1,返回值1,条件2,返回值2……最后表达式)
- select ename,job ,sal 工资涨前,decode(job,'PRESIDENT',sal+1000,'MANAGER',sal+500,sal+300) 工资涨后 from emp;
- case
- case when……then else end
- select ename , job,sal 涨前,case job when 'PRESIDENT' then sal+1000 when 'MANAGER' then sal+ 500 else sal+300 end 涨后 from emp;
```
- - - ```sql
转换函数
- 隐式转换(自动转换)
- nvarchar/char等字符 <==>number/date
- 字符转数字 select * from emp where empno = '7788';
- 日期转字符 select sysdate, instr(sysdate,'5月') from dual;
```
显示转换
- | | to number(字符,格式) | | to date(字符,格式) | |
| :--: | :-------------------: | :--: | :-------------------: | :--: |
| 数字 | <---> | 字符 | <---> | 日期 |
| | to char(数字,格式) | | to char(字符,格式) | |
- - ```sql
多行函数:进来多行,出来一行(组函数、聚合函数)
- count(*)
- 统计不重复部门编号 : select count(distinct deptno) from emp;
- 最大值 max ; 最小是 min ; 平均 avg
- 分组:**分组查询时,不在组函数(多行函数)中的列,必须在group by 中**
- **对组筛选用having,对行筛选用where**
```
查询语句的注意事项
```sql
写错之后修改:c /修改前/修改后 之后按 / 确认
或者 ed
关系型数据表:二维表
控制列: select
控制列: where
字符/字符串、日期:单引号引起来
大小写问题:
- 关键字、命令:不敏感
- 数据 : 敏感
运算符:
- 操作运算符: + -
- 关系运算符 : > >= <=
- = (等号多种意思)
- != 和 <> 不等于
- 逻辑运算符: and or not
- select * from emp where mgr = 7788 and job = 'CLERK';
- select * from emp where not (mgr = 7788 and job = 'CLERK')
- where 执行顺序 : 右--->左
去重:distinct
连接符:
- select concat('hello','Oracle') from dual;
- select 'hello'||'Oracle' from dual
修改Oracle的默认日期格式
- select * from v$nls_parameters; 查看数据字典
- alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd';
- 改回来 : alter session set NLS_DATE_FORMAT ='DD-MON-RR';