学习一下ORACEL数据库

默认端口号:1521

PL/SQL:Oracle数据库的可视化工具
Oracle里面用 || 的方式表示拼接效果
select ‘编号’ || empno||’,姓名:’|| ename from emp;

查询工资大于1500但小于3000的全部雇员
select * from emp where sal between 1500 and 3000

select * from emp where sal >= 1500 and sal <=3000

查询1981-1-1到1981-12-31号入职的员工
select * from emp where to_char(hiredate,'yyyy-MM-dd) between ‘1981-1-1’ and ‘1981-12-31’ 把日期转换成字符串类型的

select * from emp where hiredate between to_Date(‘1981-1-1’,‘yyyy-MM-dd’) and to_date(‘1981-12-31’,‘yyyy-MM-dd’) 把字符串转换成日期类型的

查询所有雇员姓名中第二个字符有’M’的雇员
select * from emp where ename like ‘_M%’

查询名字中有M的雇员
select * from emp where ename like ‘%M%’

查询雇员 编号不是7369的雇员信息
select * from emp where empno <> 7369
select * from emp where empno != 7369

查询雇员的工资进行降序排序
select ename,sal from emp order by sal desc;

查询员工的工资进行升序排序
select ename,sal from emp order by sql asc

查询员工的奖金并做降序排序
select ename.comm from emp order comm desc nulls last

查询雇员的工资做降序排序并且其中奖金部分是升序排序
select ename,sql,comm from emp order by sal desc,comm asc

将 smith转换成大写–关键字:upper
select upper (‘smith’) from dual dual虚表,没有实际意义,只为补全语法内容

将SMITH转换成小写–关键字:lower
select lower (ename) from emp;

将’smith’首字母大写–关键字:initcap
select initcap(ename) from emp;

将"helloworld"截取字符串成"hello"–关键字substr
select substr(‘helloworld’,0,5) from dual

获取"hello"字符串的长度–关键字length
select length (‘hello’) from dual;

将hello中的l用x进行替换–关键字replace
select replace (‘hello’,‘l’,‘x’)

**

数值函数

**
将15.66进行四舍五入(从-2到2)–关键字round
select round(15.66,-2) from dual; 0
select round(15.66,-1) from dual; 20
select round(15.66,0) from dual; 16
select round(15.66,1) from dual; 15.7
select round(15.66,2) from dual; 15.66

对15/3进行求余数–关键字mod
select mod(15,3) from dual

日期函数**

日期函数

**
查询系统时间–关键字sysdate
select sysdate from dual

查询雇员进入公司的周数
select ename,(sysdate-hiredate)/7 from emp

查询雇员进入公司的月数–关键字month_between
select ename,months_between(sysdate,hiredate) from emp

求出三个月后的日期–关键字add_months
select ename,hiredate,add_months(hiredate,3) from emp;

**

转换函数

**
将系统日期显示为yyyy-MM-dd hh:mi:ss (去掉补零和24小时显示时间)–关键字to_char
select to_char (‘sysdate’,‘yyyy-MM-dd’) from dual

显示年月日
select to_char(sysdate,‘yyyy年MM月dd日’) from dual

将字符串’1981-1-1’转换成日期类型–关键字to_date
select date (‘1981-1-1’,‘yyyy-MM-dd’) from dual;

**

通用函数

**

空值的处理函数
select nvl(comm,0) from emp

nvl2(判断值,空返回值,非空返回值)
select nvl2(‘xxxxxxx’,‘1’,‘2’) from dual;

**

条件表达式

**
查询员工的job内容并转成中文显示–decode方式
select ename,decode(job,‘CLERK’,‘柜员’) from emp;

**

多行函数

**
查询所有员工记录数–关键字count
select count(*) from emp;

查询佣金的总数–(如何查询某个字段的总数量)
select sum(comm) from emp;

查询最低工资–关键字min
select min(sal) from emp

查询最高工资–关键字max
select max(sal) from emp

查询平均工资–关键字avg
select avg(sal) from emp

查询20号部门的员工工资总和
select sum(sal) from emp where deptno = 20;

**

分组函数

**
查询部门编号及人数–分组查询关键字group by
select deptno,count(*) from emp group by deptno

查询每个部门编号及平均工资
select deptno,avg(sal) from emp group by deptno

查询部门名称,部门编号,平均工资
select dname,deptno,avg(sal) from dept,emp where dept.deptno = emp.deptno group by emp.deptno

查询出部门人数大于5人的部门
select deptno,count() from emp group by deptno having count() > 5

查询部门编号,部门名称,平均工资且平均工资大于2000
select emp.deptno,ename, emp.deptno,loc, grade dname,avg(sal) from emp,dept where emp.deptno = deptno group by emp.deptno,dname having avg(sal) >2000

**

多表查询

**
查询员工编号,员工姓名,员工部门编号,员工部门名称,员工部门地址,中文显示员工工资登记,及领导编号,领导姓名,领导部门编号,领导部门名称,中文显示领导登记
select from emp,deptno,dname,loc,grade where e1.deptno = dept.deptno and emp.sal between salgrade.losal and salgrade.hisal

猜你喜欢

转载自blog.csdn.net/tony_yang6/article/details/106714715