集合操作、系统自带函数

来源:互联网 发布:两张表格数据对比 编辑:程序博客网 时间:2024/06/05 19:29


--集合操作(交、并、差)  --1、交:intersect  --例如:查询部门10和部门20都有的工作类型  select job from emp  where deptno=10   intersect  select job from emp  where deptno=20 ;    --2、并:union  --例如:查询部门10的办事员和部门20的经理  select * from emp  where deptno=10 and job='CLERK' or deptno=20 and job='MANAGER';  select * from emp  where deptno=10 and job='CLERK'  union  select * from emp  where deptno=20 and job='MANAGER';  --3、差:minus  -- 例如:查询部门30中有,而部门10中没有的工作类型  select job from emp  where deptno=30  minus  select job from emp  where deptno=10;    --伪列:rowid  rownum  select rowid,rownum,deptno,dname,loc  from dept  where loc='NEW YORK';  --显示员工表前6条记录信息  select * from emp  where rownum<7;  --显示员工表7-12条记录信息  select * from emp  where  rownum<7 and empno not in(  select empno from emp  where rownum<7  );      --系统自带函数(日期、数字、字符串、类型转换):  --1、日期函数  --1)sysdate:显示系统日期时间函数  select sysdate from dual;  --2)months_between(日期1,日期2):显示两个日期之间月份的差值  --显示35年前入职的员工信息  select * from emp  where months_between(sysdate,hiredate)/12>35;  --3)add_months(日期,月份数):在指定的日期上加上月份数  select add_months(sysdate,3) from dual;  --4)next_day(日期,):显示下周几指定的日期  select next_day(sysdate,7) from dual;  --5)last_day(日期):显示日期所在月份的最后一天的日期  select last_day(sysdate) from dual;  --显示员工表中在当月倒第3天入职的员工信息  select * from emp  where last_day(hiredate)-2=hiredate;  --2、数学函数  --1)round():四舍五入  --2)trunc():截断数值  select round(56.78) from dual;  select trunc(56.78) from dual;    --显示每个员工的工龄  select ename,trunc(months_between(sysdate, hiredate)/12)  from emp;    select ceil(-56.78) from dual;    --power(m,n):m的n次方  select power(5,3) from dual;    --mod(m,n):求余数    --3、字符串函数  --lower(字符串):转小写  --upper(字符串):转大写  select lower('Hello') from dual;  select upper('Hello') from dual;    --trim():去掉左右空格  --ltrim():去掉左边空格  --rtrim():去掉右边空格    alter table emp  add email varchar2(30);    update emp set email=trim(ename)||trim('   @163.com');    select * from emp;    --lenght(字符串):求字符串长度  select length('good morning') from dual;    --4、类型转换函数  --1)to_char():  --  转换成字符串类型    select 123+5 from dual;    select to_char(123)||5 from dual;    select to_char(sysdate,'yyyy/mm/dd') from dual;  --2)to_date():  --转换成日期类型  --显示1987.5-今天入职的员工信息  select * from emp  where hiredate>=to_date('05-01-1987','mm-dd-yyyy');    select * from emp  where hiredate>='01-5月-87';  


原创粉丝点击