oracle 分组 连接查询_Oracle—多表连接,组函数,分组查询,子查询

今天学习了Oracle的多表连接,组函数,分组查询,子查询。

多表连接有三种类型:内连接,外连接,自连接。

--内连接也称为等值连接,也就是两张表之间有相同字段.

比如这里有二张表,stu clazz

select s.*,c.*

from stu s,clazz c

where s.id=c.sid;

--使用别名

select * from scott.dept,scott.emp

where scott.dept.deptno=scott.emp.deptno

and scott.emp.ename='SMITH';

-- 查询指定列

select d.deptno 部门编号, d.dname 部门名称, e.sal 薪资

from scott.dept d, scott.emp e

where d.deptno = e.deptno

and e.ename = 'SMITH';

--外连接

--分左外连接(在右边那个表中加入+,左边表格内容全部显示,右边没有的就是空值)

select * from scott.dept d, scott.emp e

where d.deptno = e.deptno(+);

select * from scott.dept d left outer join scott.emp e

on d.deptno = e.deptno;

--右外连接(在左边那个表中加入+,左右边表格内容全部显示,左边没有的就是空值)

select * from scott.dept d, scott.emp e

where d.deptno(+) = e.deptno;

select * from scott.dept d right outer join scott.emp e

on d.deptno = e.deptno;

--全外连接用标准方式写full outer join。

select * from scott.dept d full outer join scott.emp e

on d.deptno = e.deptno;

--注意用标准写法的时候,where 改为on

--自连接,自己和自己连接,把自己的表当成一个临时表。首先的先创建这么一个临时表,值跟原表一样:create table temp as(select * from stu);

--组函数:

count()统计数量

avg()计算平均值

sum()计算总和

min()最小值

max()最大值

--分组查询:当出现查询结果数量不一样多的时候,就会出现错误,这个时候使用分组查询。

group by

-- 如果想在分组后,还需要进行条件过滤

-- 可以使用 having 关键字,追加条件

select deptno, sum(sal)

from scott.emp

group by deptno

having sum(sal) > 10000;

-- 常见的关键字使用顺序:

-- select > from > where > group by > having > order by

--nvl函数 如果第一个参数为null,则取第二个参数

select comm from scott.emp;

select nvl(comm,0) from scott.emp;

--使用0替换null的comm,计算年收入

select ename,sal,comm,(sal + comm)*12 年收入 from scott.emp;

select ename,sal,comm,(sal + nvl(comm,0))*12 年收入 from scott.emp;

--nvl2函数 如果第一个参数为null,则取第三个参数,否则取第二个参数

select nvl2(comm,comm,0) from scott.emp;

--nullif 相等返回NULL,不等返回expr1

select nullif(1,2) from dual;

三个函数to_char\to_date\to_number

select to_date('2018-08-30','yyyy-mm-dd') from dual;--设置日期格式·

select to_char(sysdate,'yyyy-mm-dd') from dual;--设置日期字符串

select to_char(sysdate,'yyyy/mm/dd') from dual;

select to_char(sysdate,'mm/dd') from dual;

to_char函数,可以将日期转成字符串,我们可以从中拿取年、月(一般)

比如:查询某公司4月份销售总量的问题。

select to_char(1233.5566) from dual;--数字转成字符串

select to_number('1233.687') from dual;--非数字转为数字

--两个日期之间还可以进行减法

--得到相差的天数

--还可以是负数

select sysdate-to_date('2018/07/15','yyyy-mm-dd') from dual;

select sysdate - to_date('2016-08-20','yyyy-mm-dd') from dual;

select TRUNC(sysdate - to_date('2016-08-20','yyyy-mm-dd')) from dual;

select to_date('2016-08-22 14:27:00','yyyy-mm-dd hh24:mi:ss') - sysdate from dual;

--可以用数字除24来向日期中加上或减去小时。

select sysdate+2/24 from dual;

--select sysdate/24+2 from dual;(错误)

--可以用数字除24再除60来向日期中加入分钟

select sysdate+2/24/60 from dual;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值