子查询:内查询,嵌套查询

括号里面的查询语句会先于主查询语句

复制代码
create table info (
id int,
name varchar(10) primary key not null,
score decimal(5,2),
address varchar(20),
hobbid int(5)
);

select name,score from info where name in (select name from info where score > 80);
子查询返回的结果只能是1列
where条件in什么,子查询列表就是什么

create table zu1(

id int(4)

);

select id,name,score from info where id in (select id from zu1)

这两张表有想用的名字,查询成绩

select id,name,score from info where name in (select name from zu1)

多表联查,不要超过三张

select id,name,score from info where id not in (select id from info where score <70);

not in 取反

子查询语句还可以用在insert update delete

insert into test select * from info where id in (select id from info where sex='女')

插入数据要求按照地址,包含南京插入到test

select * from test;

insert into test select * from info where id in (select id from info where address='南京')

update info set score=50 where id in (select id from test where id = 5)

select * from info;

修改info表score=100 not in 子查询的条件 id > 1

update info set score=100 where id not in (select id from test where id > 2)

delete from info where id in (select id where score > 80);

select * from info;

exists:关键字在子查询时,主要用于判断子查询的结果集是否为空。不为空,返回true

根据info表,查询大运80分的同学然后统计有多少个

select count(*) from info a

where exists(select id from info where score > 80 and info.id=a.id);

select count(*) from info a where exists(select id from info where score <80 and info.id=a.id)

视图:mysql当中的视图 VIEW

视图在mysql当中是一个虚拟表,基于查询结果得出的一个虚拟表

当工作当中,我们查询的表未必时真表,有可能时是基于真表查询结果的一个虚拟表

可以简化负载的查询语句,隐藏表的细节,提供安全的数据访问

创建是视图表可以是一张表的结果集,也是多个表共同的查询的结果表

create view test2 as select * from info where score >=80;

desc test2;

desc info;

视图表和真表的区别

1、存储方式不一样的,真表存储实际数据,真正的写在磁盘当中的,视图不存储任何数据

仅仅是一个查询结果集的虚拟表

2、数据更新的角度来说表可以增可以删可以改可以查但是视图一般情况只能用于查,展示数据

3、占用空间,真表真实占用空间,视图不占用数据库空间的。

查询视图表

show full tables in kgc1 where table_type like 'view'

删除是视图表

drop view test2

create table test01 (

id int,

name varchar(15),

age int(3)

);

info和test01

根据info的id,name score,加上test01的age?

create view v_info(id,name,score,age) as

select a.id,a.name,a.score,b.age from info a,test01 b

where a.name=b.name;

select * from v_info

update info set score=90 where name = '阿金'

源表的数据发生变化,视图表的数据同步更新

create view

update v_info set age=60 where name = '阿伟'

修改了视图表,源表的数据也会发生变化,一般不会对视图表进行改动操作

select * from test01

视图表适用于安全性比较高的场景

null值和空值:

null就是真空

空值类似于空气

select * from info where address is not null

select count(address) from info;

null是不被统计的,空值可以被统计。

连接查询:

内连接:

是把两张表或者多张表(三张),同时符合特定条件的数据记录的组合。

一个或者多个列的相同值,才会有查询的结果。

select a.id,a.name from test01 a inner join info b on a.id=b.id;

select a.id,a.name from test01 a inner join info b on a.name=b.name;

左连接:

左外连接,在left join 关键字来表示。在左连接当中国,左侧表是基础表,

接受左表的所有行,然后和右表的(参考表)记录进行匹配

匹配左表当中的所有行,以及右表中符合条件的行。

select a.name,a.id,b.name,b.id from test01 a left join info b on a.name=b.name

右连接:

右外连接 right join以右侧表为基础,接受右侧表的所有记录,匹配的记录,不匹配的记录null值

select * from test01 a right join info b on a.id=b.id

select a.name,a.id,b.id,b.name from test01 a right join info b on a.id=b.id

相关推荐
不辉放弃11 分钟前
java连数据库
java·mysql
GzlAndy15 分钟前
MySQL全局优化
数据库·mysql
m0_7415747518 分钟前
mysql主从同步
数据库·mysql
小白教程1 小时前
MySQL数据库的安全性防护
数据库·mysql
Lion Long1 小时前
CodeBuddy 中国版 Cursor 实战:Redis+MySQL双引擎驱动〈王者荣耀〉战区排行榜
数据库·redis·mysql·缓存·腾讯云·codebuddy首席试玩官·codebuddy
apcipot_rain4 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
辛一一7 小时前
neo4j图数据库基本概念和向量使用
数据库·neo4j
LJianK18 小时前
关系型数据库和非关系型数据库
sql
巨龙之路8 小时前
什么是时序数据库?
数据库·时序数据库
蔡蓝8 小时前
binlog日志以及MySQL的数据同步
数据库·mysql