子查询:内查询 嵌套查询
#select 嵌套 (嵌套) ,括号里面的查询语句会先于查询语句执行,然后再把子查询的结果作为条件返回给主查询条件进行过滤
语法
mysql> select * from info;
+----+--------+--------+--------------+-----+
| id | name | score | address | sex |
+----+--------+--------+--------------+-----+
| 1 | 郭旗 | 100.00 | 北京西路 | 男 |
| 2 | 韩文 | 90.00 | 南京西路 | 男 |
| 3 | 王浩 | 60.00 | 东京西路 | 男 |
| 4 | 杨树 | 99.00 | 山东西路 | 女 |
| 5 | 姚美 | 98.00 | 山西西路 | 女 |
| 6 | 刘亚 | 10.00 | 四川西路 | 男 |
| 7 | 袁野 | 11.00 | 内蒙西路 | 女 |
| 8 | 刘文 | 100.00 | 河南西路 | 男 |
+----+--------+--------+--------------+-----+
#举例
select name,score from info where id in (select id from info where score > 80);
+--------+--------+
| name | score |
+--------+--------+
| 郭旗 | 100.00 |
| 韩文 | 90.00 |
| 杨树 | 99.00 |
| 姚美 | 98.00 |
| 刘文 | 100.00 |
+--------+--------+
select id,name,score from info where id in (select id from ys66);
#查询ys66的id列,将它ys66查询出来的id列传给info表之后在进行过滤,这两张表右相同的名字,查询成绩
select id,name,score from info where name in (select name from ys66)
#查询ys66的name列
select id,name,score from info where id not in (select id from info where score > 70);
#子查询里info表需要加上where语句大于70分取反,也就是小于70分显示出来。insert into
insert into test select * from info where id in (select id from info where sex='女');
#info表sex列=女,才会插入test表
例:插入一个数据,要求按地址,包含南京插入到test
insert into test select * from info where id in (select id from info where address like
'南京');
#这个地方不能直接等于南京,只能用like '南京' 如果用=结果会有偏差
update
update info set score=50 where id in (select id from ys66 where id = 2);
#更新info表 set指定 成绩=50 ,通过 id 子查询 ys66 表 的id列 2确定,
update info set score=100,where id not in (select id from ys66 where id > 1);
# 更新info表 score=100 , 只要id列不是大于1的列都更新exists
关键字在子查询时,主要用于判断子查询的结果集是否为空,不为空返还true,为空则返回false
举例:根据info表,查询大于80分的同学,然后统计有多少个
select count(*) from info where exists (select id from info where score > 80);
#只要有一个条件满足,那么就会判断为真,将所有的数据都展示
select count(*) from info a where exists (select id from info where score > 80 and =);视图view
#视图:mysql当中的视图view
#视图在mysql当中是一个虚拟表,基于查询结果得出的一个虚拟表
#在工作中,查询的表未必是真表,有可能是基于真表查询结果的一个虚拟表,它有个好处,可以简化复杂的查询语句,隐藏表的细节,提供安全的数据访问
#创建视图表可以使一张表的结果集,也可以是多个表的共同的查询的结果集
create view test2 as select * from info where score >= 80;
#创建虚拟表test2 数据从查询info表获得,但score必须大于等于80 才会被写入虚拟表test2
视图表和真表之间的区别
1.存储方式不一样,真表存储实际数据,真正写在磁盘当中,视图不存储任何数据,仅仅是一个查询结果集的虚拟表
2.真表可以增删改查,但是视图一般情况只能用于查,展示数据
3.占用空间,真表真实占用空间,视图是不占用数据库空间
举例
show full tables in ys66 where table_type like 'view';
#查询ys66库 哪些表的类型是 视图:虚拟表
drop view v_score;
#删除视图:虚拟表
#根据多个表来创建视图表(虚拟表)
#info和test01
#根据info的id,name,score 加上test01的age
create view v_info(id,name,score,age) as select ,,a.score,b.age from info a, test01 b where =;
#select 跟上info的id name score和test01的age别名,再定义info的别名a和test01的别名b,最后加上约束(info别名)等于(test01别名)
update info set score=90 where name = '胡';
#源表的数据发生变化,视图表的数据同步更新
uodate v_info set age=100 where name = '王宇';
#修改了视图表,源表的数据也会发生变化,一般情况下我们是不对视图表进行改的操作
真表占了80%,视图适用于安全性比较高的场景,对外访问,基本都是视图null值和空值
select * from info where score is not null;
select * count(address) from info;
#null是不被统计的,空值可以被统计连接查询:内连接,左连接,右连接
#内连接
#把两张表或者多张表(最多三张),同时符合特定条件的数据记录的组合。
#一个或者多个列相同值才会有查询的结果
select , from test1 a inner join info b on =;
#左连接
#又叫左外连接,用left join 关键字来表示,在左连接当中,左侧表是基础表
#接收左边的所有行,然后和右表(参考表)记录进行匹配
#匹配左表当中的所有航,以及右表中符合条件的行,不匹配的部分 记录 null值
select * from test1 a left join info b on =;
#test1 为主表
#右连接
#右外连接,right join 以右侧表为基础,接收右侧表 的所有记录,匹配的记录,不匹配的记录null值
select * from test1 a right join info b on =;
#info为主表
















