表查询(下)
一 、联合分组
按多个字段综合结果进行分组
#数据来源:在单表student下
#按 area与port组合后的结果进行分组,只有组合后的结果还一致,才认为是一组
eg:
select group_concat(name),area,port from emp group by area,port;
--------------------+--------+-----------+
| group_concat(name) | area | port |
+--------------------+--------+-----------+
| yangsir,san1,san | 上海 | 浦东 |
| san2 | 上海 | 浦西 |
| ruakei | 上海 | 陆家嘴 |
| zero | 中国 | 黄浦 |
| owen | 安徽 | 宣城 |
| jason | 安徽 | 巢湖 |
| ying | 安徽 | 芜湖 |
| kevin,engo | 山东 | 济南 |
| monkey | 山东 | 青岛 |
| tank | 广州 | 广东 |
| jerry | 江苏 | 张家港 |
| jiboy | 江苏 | 苏州 |
+--------------------+--------+-----------+
二、子查询
子查询:将一条查询sql的结果作为另一条查询sql的条件
增
#insert into 表 select 子查询
查
# select fron 表 where() in (select 子查询)[表不能与delete表相同]
'''查每个学校身高最高的那个人的全部信息'''
1)#子查询的sql
select school,max(height) from student group by school;
#先查询出每个学校的最大身高
+--------+-------------+
| school | max(height) |
+--------+-------------+
| 北大 | 183.1 |
| 复旦 | 183.3 |
| 清华 | 185.9 |
+--------+-------------+
2)#查
select * from student where (school,height) in (select school,max(height) from student group by school);
+----+------+--------+--------+--------+--------+--------+
| id | name | gender | height | area | port | school |
+----+------+--------+--------+--------+--------+--------+
| 2 | bbb | 男 | 183.1 | 上海 | 青浦 | 北大 |
| 3 | ccc | 男 | 183.3 | 北京 | 朝阳 | 复旦 |
| 4 | ddd | 男 | 185.9 | 广东 | 广州 | 清华 |
+----+------+--------+--------+--------+--------+--------+
# 将子查询转换为一张表
1)# 创建一个存子查询数据的一张表
create table school_height(
school_name varchar(64),
max_height varchar(64));
2)#将子查询的数据增加到school_height表中
insert into school_height select school,max(height) from student group by school;
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大 | 183.1 |
| 复旦 | 183.3 |
| 清华 | 185.9 |
+-------------+------------+
3)#需求(同样也会实现上面的需求)
select name,school_name,height
from student join school_height
on student.school = school_height.school_name
and student.height = school_height.max_height;
+------+-------------+--------+
| name | school_name | height |
+------+-------------+--------+
| bbb | 北大 | 183.1 |
| ccc | 复旦 | 183.3 |
| ddd | 清华 | 185.9 |
+------+-------------+--------+
改
# update 表 set 字段=值 where 字段 in (select 子查询)[表不能与delete表相同]
'''每个部门最大身高加一'''
update school_height set max_height=max_height+1;
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大 | 184.1 |
| 复旦 | 184.3 |
| 清华 | 186.9 |
+-------------+------------+
'''给school_height表额外增加一个新学校'''
insert into school_height values ('南开', 188);
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大 | 184.1 |
| 复旦 | 184.3 |
| 清华 | 186.9 |
| 南开 | 188 |
+-------------+------------+
'''更改'''
update school_height set max_height=max_height+1 where school_name in (select distinct school from student);
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大 | 185.1 |
| 复旦 | 185.3 |
| 清华 | 187.9 |
| 南开 | 188 |
+-------------+------------+
#You can't specify target table 'school_height' for update in FROM clause
#报错,update更新的表 与 子查询select的表 相同
update school_height set max_height=max_height+1
where school_name in
(select distinct school_name from school_height);
删
#delete from 表 where() in (select 子查询)[表不能与delete表相同]
delete from school_height where school_name in (select distinct school from student);
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 南开 | 188 |
+-------------+------------+
#错误: delete删除的表 与 子查询select的表 相同
delete from school_height where school_name in (select distinct school_name from school_height);
三、区间修饰条件:all与any
all:要满足所有的条件
where salary < all(3, 6, 9) => salary必须小于所有情况
any:满足其中一个条件就行
where salary < any(3, 6, 9) => salary只要小于一种情况
eg:
select * from student where height < all(select height from student where id<4);
#先找到id<4的学生的身高,分别为 173.1,183.1,183.3,之后再拿身高作为参照物,找出满足所有满足身高条件(身高小于173.1)的同学
+----+------+--------+--------+--------+--------------+--------+
| id | name | gender | height | area | port | school |
+----+------+--------+--------+--------+--------------+--------+
| 5 | eee | 女 | 168 | 山东 | 烟台 | 北大 |
| 6 | fff | 女 | 165 | * | 乌鲁木齐 | 北大 |
| 8 | hhh | 女 | 166.1 | 广州 | 广东 | 复旦 |
| 13 | ppp | 女 | 155 | 江苏 | 连云港 | 清华 |
+----+------+--------+--------+--------+--------------+--------+
四、视图:view
视图的增删改操作可以直接映射给真是 表(本质就是对真是表进行操作)
- 视图是存在内存中的临时表
- 视图的创建依赖select语句
- 视图支持对数据的增删改查
- 视图不允许对视图表的字段做修改
- 视图不仅支持创建,也支持更新与删除
'''创建视图'''
'''create view 视图名[(别名们)] as select 语句'''
create view 视图名[(别名们)] as select 语句;
eg:
create view v1 as select
school,max(height)
from student group by school;
+--------+-------------+
| school | max(height) |
+--------+-------------+
| 北大 | 183.1 |
| 复旦 | 183.3 |
| 清华 | 185.9 |
+--------+-------------+
'''创建或替换视图'''
''' create or replace 视图名[(别名们)] as select 语句;'''
create or replace view v1(school_name, max_height) as select school, max(height) from student group by school;
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大 | 183.1 |
| 复旦 | 183.3 |
| 清华 | 185.9 |
+-------------+------------+
''' alter 视图名[(别名们)] as select 语句;'''
alter view v1(name, height) as select school, max(height) from student group by school;
+--------+--------+
| name | height |
+--------+--------+
| 北大 | 183.1 |
| 复旦 | 183.3 |
| 清华 | 185.9 |
+--------+--------+
'''删除视图'''
drop view 视图名
eg:drop view v1;
'''视图可以作为正常表完成连表查询'''
eg:create or replace view v2 as select name,gender,height from student;
select * from v2;
+------+--------+--------+
| name | gender | height |
+------+--------+--------+
| aaa | 女 | 173.1 |
| bbb | 男 | 183.1 |
| ccc | 男 | 183.3 |
| ddd | 男 | 185.9 |
| eee | 女 | 168 |
| fff | 女 | 165 |
.........
视图的增删改
视图可以完成增删改,增删改本质是对真实的表进行操作
#先创建一个视图
create or replace view v2 as select id,name,gender,height from student;
#改
update v2 set height=height+1 where id=1; #两个表都改变了
select * from v2;
+----+------+--------+--------+
| id | name | gender | height |
+----+------+--------+--------+
| 1 | aaa | 女 | 174.1 |
select * from student;
+----+------+--------+--------+-----------+--------------+--------+
| id | name | gender | height | area | port | school |
+----+------+--------+--------+-----------+--------------+--------+
| 1 | aaa | 女 | 174.1 | 甘肃 | 张掖 | 清华 |
#删
deleate from v2 where id = 1;
#增
create or replace view v3 as select * from student;
insert into v2 values(18,'wwb','男','183','上海','青浦','北大');
#操作视图会影响真实表,反之也会影响
update sutdent set height = height+1 whereid= 1;
五、事务
通常一些业务需要多条sql参与,参与的sql会形成一个 执行整体,该整体我们就称之为事务(事务就是包含多条执行的sql语句)
5.1事务的四大特性
- 原子性:所有的执行都要一次性成功,不能有任何一个执行失败,要么同时成功,要么同时失败
- 一致性:事务前后的数据完整性应该保持一致
- 隔离性:支持并发,执行可以同时进行,数据不会乱
- 持久性:一旦事务被提交,他对数据的改变就是永久性的,接下来如果数据库有故障,也不会对提交的数据有影响
create table bank(
id int primary key auto_increment,
name varchar(16),
money decimal(65,2));
insert into bank(name,money) values('yjy',10),('wwb',20);
#假设出现以下执行情况
update bank set money = money-1 where name = 'yjy';
update bank set money = money+1 where name = 'wwb';
'''没有事务支持下,yjy的钱就丢了'''
#这种执行是错误的,因为ypp不存在,yjy的银行账户钱转丢了
update bank set money = money-1 where name = 'yjy';
update bank set money = money+1 where name = 'ypp';
'''将2条sql看做事务处理'''
#开启事务
begin;
update bank set money = money-1 where name = 'yjy';
update bank set money = money+1 where name = 'ypp';
#确认无误,提交事务
commit;
#确认有误,回滚事务
rollback;