Oracle查询以及函数

来源:互联网 发布:菜鸟网络 高科技园区 编辑:程序博客网 时间:2024/04/29 00:54
-------------------------查询---------------------------------------
-- scott
-- 提供一个虚表dual
select 1+2 from dual
-- 1.oracle 连接符  ||
select '你'||'好' from dual;


--查询
select '工资'||(sal+comm) as salary  from emp where empno = 888;


-- 去重复项
select distinct job from emp;-- 一般只查询一个字段


--1 in ,not in,可以写多个值
--2 =,>,>=,<,<=,!=,like 后面绝对只有一个值
--3 is null/is not null
select * from emp where empno in (7369,888,7777);-- 可以设多个值
select * from emp where empno = 7369;


-- 返回多个子集合
select * from emp where empno in 
(
select empno from emp where sal =5000
);


-- 没有奖金的人查询出来
select * from emp where comm is null;


//exists not exists-- 了解
select * from emp where exists 
(
select * where emp where sal =5000--查询多个字段只能用exists
);


------------------------函数查询--------------------------------
-- 查询员工的入职时间,生成varchar2类型,对应java的String
--to_char
select to_char(HIREDATE,'yyyy-mm-dd') from emp;


-- 字符,转化成日期to_date                        dual是虚表
select to_date('2008/01/01','yyyy-mm-dd') from dual;


-- 时间的比较
select * from temp where hiredate <=to_date('1987/04/28','yyyy-mm-dd');




-- 插入一条时间
insert into userinfo(userid,username,userage,usersex,userbirthday) values(2,'李狗蛋',18,'男',to_date('2016-08-27','yyyy-mm-dd'));


-- 插入时间
insert into temp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(8000,'zhang1','xiaoshou1',7369,to_date('2016/10/13 12:12:12','yyyy-mm-dd hh24:mi:ss'),1500,100,20);




-- 数字to_number
select to_number('100') from dual;-- Integer.parseInt();


--空转其它数字
-- nvl,nvl2
 -- 请计算每个员工的总月工资
select sal+nvl(comm,0) from emp;-- 如果comm是空的,就取值0


select sal+nvl2(comm,1000,0) from emp;
-- 如果不空,就加1000(comm=1000),空就加0


-- select decode(varchar,,,,,,,) 别名 from 表名        decode---译码,解码
-- select decode(字段名,,,,,,,) 别名 from 表名


select decode(to_char(hiredate,'MM'),
'01','一月', '02','二月','03','三月','04','四月',
'05','五月','06','六月','下半年') mon 
from emp;


-- 假设有一张表userinfo(sex字符类型),存储的是0,1
select decode(sex,'0','男','1','女') sex from userinfo;-- sex是char
select decode(sex,0,'男',1,'女') sex from userinfo;-- sex是number




select decode(deptno,10,'教学部',20,'销售部',30,'营销部') depart from empno;


-- 不用decode函数,直接用select case语句
select deptno from emp;
select 
case deptno
     when 10 then '教学部'
     when 20 then '销售部'
     when 30 then '营销部'
     else '其它部门'
end depart
from emp;
0 0
原创粉丝点击