Oracle学习第一天

select * from emp;
/*
select * from emp where comm is not null;

select 7*8 from dual;


select sal from emp where sal between 2000 and 4000;


select ename from emp where ename like '__O%';--第三个字符是O的


update emp set ename = 'TUR%NET' where ename = 'TURNER';

select * from emp where ename like '%"%%' escape '"';--escape是指下面的那个符号是转译符(可以换成别的符号)

select * from emp order by comm desc ;
select * from emp order by comm desc nulls last;--排序中的null默认排在首行,这样可以要他到最后

select deptno,sal from emp order by deptno asc ,sal desc; 
*/
/*
 函数:必须要有返回值;
 单行函数:对某一行中的某个值进行处理
      数值函数 ceil,floor,round,trunc
      字符函数
      日期函数
      转换函数
      通用函数
 多行函数:对某一列的所有行进行处理
 max() min count sum avg
 
*/
/*
--统计员工工资总和
select sum(sal) from emp;

--统计员工奖金总和 2200 (直接忽略空值)
select sum(comm) from emp;

--统计员工人数 14
select count(*) from emp;


--统计员工平均奖金 550这里不忽略空值就会出错
select avg(comm) from emp;

--统计员工的平均奖金 157
select (sum(comm)/count(1)) from emp;


--数值函数
select ceil(45.434) from dual;--向上取整
select floor(45.66) from dual;--向下取整

--四舍五入
select round(45.65,1) from dual;--四舍五入
select round (45.65,-1) from dual;--这个是精确到十位数

--截断
select trunc(45.65,1) from dual;

--求余
select mod(9,2) from dual;

--字符函数substr(str1,起始索引,长度) 起始索引不管写0还是1 都是从第一个字符开始截取
select substr('abcdef',0,3) from dual; --abc
select substr('abcdef',1,3) from dual;--abc


--获取字符串的长度length
select length('abcde') from dual;--5


--去除字符左右两边的字符trim
select trim('   hell o  ') from dual;--可以删除前后的空格

select floor(-12.5) from dual;---13

select trim ('0' from '00232004100') from dual;--可以自定义去除

--替换字符串
select replace('hello','l','g') from dual;

--日期函数
select sysdate from dual;
--查询3个月后今天的日期
select add_months(sysdate,3) from dual;
--查询3天后的日期
select sysdate + 3 from dual;

--查询员工入职时间
select ceil(sysdate - hiredate) from emp;

--查询员工的周数
select (sysdate - hiredate)/7 from emp;

--查询员工入职的月数
select months_between(sysdate,hiredate) from emp;

--转换函数 数值转字符 字符转数值 日期
--字符转数值to_number(str) --鸡肋,基本不会用
select 100+'10' from dual;--默认已经帮我们转了
select '10'+100 from dual;

select to_char(sal,'$9,999.99') from emp;


--日期转字符
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'d') from dual;--代表一个星期的第几天
select to_char(sysdate,'dd') from dual;--代表一个月中的第几天
select to_char(sysdate,'ddd') from dual;--代表一年中的第几天
select to_char(sysdate,'day') from dual;--代表星期几
select to_char(sysdate,'dy') from dual;--代表星期几缩写

--字符转日期
select to_date('2017-04-20','yyyy-mm-dd') from dual;

--查询1981年到1985年入职的员工信息
select * from emp where hiredate between to_date('1981','yyyy') and to_date('1985','yyyy');
*/

--通用函数
/*
nvl(参数1,参数2) 如果参数1 = null,就返回参数2
nvl2(参数1,参数2,参数3) 如果参数1 = null 就返回参数3 否则返回参数2
nullif(参数1,参数2) 如果参数1 = 参数2 那么返回null,否则返回1


select nvl(comm,0) from emp;
select nullif(5,5) from dual;

select coalesce(null,null,3,4,5) from dual;--返回第一个不是null的数
*/
/*
条件表达式
case 字段:
  when 值1 then 值
  when 值2 then 值
  else
      默认值
  end
  
  
  
  case .. when 通用的写法oracle和mysql都可以使用
    oracle 的特有写法decode(列名,值1,值1.1,值2,值2.1,值3.1)

select
case ename
  when 'JONES' then '约翰'
  when 'WARD' then '刘备'
  else '路人'
  end 
 from emp;
 
 select decode(ename,'JONES','约翰','WARD','刘备','路人')from emp;
*/
/*
分组表达式group by
select 分组的条件,分组之后的操作 from 表名 group by 分组的条件 having 条件过滤
*/
--分组统计所有部分的平均工资,找出平均工资大于2000的部门
select deptno,avg(sal) avgsal from emp group by deptno having avg(sal) > 2000;
/*
--sql的编写顺序
--select .. from .. where .. group by .. having .. order by
--sql的执行顺序
--from .. where .. group by .. having .. select .. order by
*/

猜你喜欢

转载自www.cnblogs.com/pengzhi/p/10533548.html