Table query (at)
A joint grouping
Grouped by the combined result of multiple fields
#数据来源:在单表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 | 江苏 | 苏州 |
+--------------------+--------+-----------+
Second, sub-queries
Subquery : The sql query results as a condition of the other sql query
increase
#insert into 表 select 子查询
check
# 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 |
+------+-------------+--------+
change
# 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
#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);
Third, the interval modification conditions: all and any
all: to meet all of the conditions
where salary <all (3, 6, 9) => salary all cases must be less than
any: meet one of the conditions on the line
where salary <any (3, 6, 9) => salary as long as the case is less than
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 | 江苏 | 连云港 | 清华 |
+----+------+--------+--------+--------+--------------+--------+
Fourth, view: view
CRUD operations can be mapped directly to the views really table (essentially to really operating table)
- A view is stored in memory temporary table
- Create dependency view select statement
- View support data additions and deletions to change search
- View do not allow changes to the field of view of the table
- A view not only to support the creation, update and deletion is also supported
'''创建视图'''
'''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 |
.........
CRUD views
CRUD complete view, essentially deletions change is real operating table
#先创建一个视图
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;
Fifth, the transaction
Usually some businesses require multiple sql participation, involvement sql execution will form a whole, which we call the whole transaction (a transaction is executed sql statement contains multiple)
5.1 four characteristics Affairs
- Atomicity: all must perform a one-time success, you can not have any failed or successful while, at the same time either fail
- Consistency: Data integrity before and after the transaction should be consistent
- Isolation: support concurrent execution can be carried out at the same time, the data is not chaos
- Durability: Once a transaction is committed, he changed data to the data is permanent, then the database if there is a fault, it would not submit an impact
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;