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

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

复制代码
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

相关推荐
Nyarlathotep01131 小时前
事务隔离级别
sql·mysql
悟空聊架构1 小时前
基于KaiwuDB在游乐场“刷卡+投币”双模消费系统中的落地实践
数据库·后端·架构
IvorySQL1 小时前
PostgreSQL 技术日报 (3月4日)|硬核干货 + 内核暗流一网打尽
数据库·postgresql·开源
Nyarlathotep01134 小时前
SQL的事务控制
sql·mysql
进击的丸子4 小时前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
用户86178277365185 小时前
MySQL 8.0从库宕机排查实录:中继日志膨胀引发的连锁故障复盘
mysql
NineData20 小时前
NineData智能数据管理平台新功能发布|2026年1-2月
数据库·sql·数据分析
IvorySQL21 小时前
双星闪耀温哥华:IvorySQL 社区两项议题入选 PGConf.dev 2026
数据库·postgresql·开源
ma_king1 天前
入门 java 和 数据库
java·数据库·后端
jiayou641 天前
KingbaseES 实战:审计追踪配置与运维实践
数据库