求一下总金额
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’
Oracle SQL基础之(二) [联合查询、union、子查询、外、内连接、复制表]
最新推荐文章于 2021-04-04 16:50:31 发布