Oracle SQL基础之(二) [联合查询、union、子查询、外、内连接、复制表]

求一下总金额
select sum(price*qty) from books;

进行分组查询(后面group by中的必须要在select中)
Select sum(price*qty) from books group by pub;
Group by和having结合代替where
错误的案例:
这是不对的,就像前面所说的聚合函数不能喝where相结合
Select pub,sum(price*qty) from books where sum(price)>30
所以我们用having子句去结合,如下所示
Select pub,sum(price*qty) from books group by pub having sum(price)>500


查询以a字符开头,以任意单个字符结尾的。
Select * from aa where a1 like ‘a_’

查询以什么a字符开头的记录,以任意单个字符结尾的。
Select * from aa where a1 like ‘a__’ 下划线长一些


Select * from aa where a1 like ‘a%’  任意一些的
如果不知道字符在哪个位置。也就是模糊查询
Select * from aa where a1 like ‘%a%’;

内连接
同时显示部门名称和员工信息。Select两个表的写法,内连接。公共匹配的
数据
简化的写法
Dept表,employee表
Select d.name,e.name,sex from employee e,dept d where e.id=d.id
标准的写法:
Select d.name,e.name,sex from employee e join dept d on e.id=d.id

外连接:
左连接:
左边员工表的记录全部显示。右边表只显示匹配的,相同的部分显示一次
Select d.name,e.name,sex from employee e,dept d where e.id=d.id(+)
右连接正好相反
左连接和右连接合成为外连接

子查询
In写法和下面的exists是一个意思
Select * from employee e where id not in (select id from dept where id=’3’);
Select * from employee e where id in (select id from dept where id=’3’);

Exists写法和上面的in是一个意思,exists可以用*来匹配。
Select * from employee e where not exists (select * from d where id=e.id)
Select * from employee e where exists (select id from d where id=e.id)


union合并行数据,只是显示,不变化物理数据
select id from e
union
select id from d;
相同的行数据合并。不同的只显示一次


将两个表中合并显示。只是显示相同的行数据
Select id from e
Intersect
Select id from d


用别的表的结果集插入到别的表中
Insert into e(eid,ename) select id,name from d;

复制表
将一行数据放进去表中,把已有表的字段和数据行
放到一个表中用来复制
Create table ttt as select e.id,e.name from e where eid=’001’

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值