------------------------1.简单的SQL查询--------------------------
select * from emp;
select empno,ename,job from emp;
select ‘编号是:‘|| empno || ‘ 名称是:‘|| ename from emp;
select distinct empno ,ename from emp;
select empno,ename,job,sal*100 income from emp;
------------------------2.限定查询--------------------------
select * from emp where sal>2000;
select * from emp where not(sal>2000);
select * from emp where mgr is not null;
select * from emp where sal between 0 and 1000;
select * from emp where sal in(800,900);
select * from emp where sal not in(800,900);
select * from emp where sal >1000;
select * from emp where sal <>100;
select * from emp where ename like ‘SCO%‘;
------------------------3.排序------------------------------
select * from emp order by sal asc;--升序
select * from emp order by sal desc,Hiredate asc;--按sal进行降序,如果sal相同则安排hiredate的早晚排序
------------------------4.单行函数------------------------------
--1.>字符函数:专门处理字符
--大小写转换
select * from emp where ename=upper(‘allen‘);
select lower(ename) from emp;
--首字母大写。
select empno,initcap(ename) from emp;
select initcap(‘hello,world‘) from dual;
--字符串截取
select substr(ename,1,2) from emp;--截取前两位
select substr(ename,0,2) from emp--0/1开始都一样
select substr(ename,length(ename)-2) from emp;--字符最后两位
--字符长度
select length(ename) from emp;
--字符替换
select replace(ename,‘S‘,‘5‘) from emp;
--2.>数值函数
--四舍五入
select round(9.536) from dual;--10
select round(9.536,2) from dual;--9.54
--截断小数点,直接截取,不做四舍五入
select trunc(9.536) from dual;--9
select trunc(9.536,2) from dual;--9.53
--取余
select mod(10,3) from dual;--1
---3>.日期函数
select sysdate from dual;
select sysdate-1 from dual;
select * from emp;
--查询10部入职的星期数
select empno,ename,round((sysdate-hiredate)/7)week from emp;
---3.1 MONTHS_BETWEEN() 求两个日期之间的月数。
select round(months_between(sysdate,hiredate)) from emp;
---3.2 ADD_MONTHS() 在指定日期,上加月数
select add_months(sysdate,1) from dual;
---3.3 NEXT_DAY() 下一个今天是什么日期
select next_day(sysdate,‘星期六‘) from dual;--从今天开始,找下一个星期六
---3.4 LAST_DAY() 求出给定日期的最后一天
select last_day(sysdate) from dual;--当前月最后一天
---4>.转换函数
--4.1 to_char转换字符
select empno,ename,to_char(hiredate,‘yyyy-mm-dd‘) d from emp;--2016-06-18
select empno,ename,to_char(hiredate,‘fmyyyy-mm-dd‘) d from emp;--2016-6-18,fm去掉年月日前的0;
select empno,ename,to_char(sal,‘999,90.00‘) from emp;--对数值格式化
select empno,ename,to_char(sal,‘$999,90.00‘) from emp;
select empno,ename,to_char(sal,‘L999,90.00‘) from emp;--对数值格式化,L表示,本地币种
--4.2 to_number 转换数值
select to_number(‘123‘) + to_number(‘123‘) from dual;--246
--4.3 to_date 转换日期
select to_date(‘2016-06-18‘,‘yyyy-mm-dd‘) d from dual;
---5>.通用函数
select ename,nvl(comm*2,0) from emp;--将空值变为指定内容(0).
select decode(3,1,‘aa‘,2,‘bb‘,3,‘cc‘) from dual;--cc 类似if.. elseif.. else
select empno 编号,ename 姓名,decode(job,‘CLERK,‘,‘业务员‘,‘SALESMAN‘,‘销售人员‘,‘ANALYST‘,‘分析员‘,‘MANAGER‘,‘经理‘,‘PRESIDENT‘,‘总裁‘)职位 from emp;