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

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

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

相关推荐
草莓熊Lotso1 小时前
【C++ 基于微服务的即时通讯系统】etcd + brpc 从安装到服务注册发现全链路实战
网络·数据库·c++·redis·微服务·etcd
Lethehong6 小时前
Oracle 加个字段,目标库就报错?KFS 用“先拆后组“打通异构 DDL
数据库·oracle
Web极客码7 小时前
锁定真正的磁盘卡顿:我用 OpenCode 配合 eBPF 诊断 NVMe 队列深度延迟与 I/O 写放大
数据库·oracle
cspttty7 小时前
2027用户研究求职准备:应用统计学如何补齐研究设计与业务表达
大数据·数据库
roman_日积跬步-终至千里8 小时前
【数据Agent论文·查询与语义|05】Spider:为什么换一个数据库,模型就不会查了?
数据库·数据分析
猫咪很可爱9 小时前
Oracle RAC 启动失败排查:私网通信与内核参数优化实战
运维·数据库·oracle
XLYcmy9 小时前
《数字经济赋能城市低碳治理的路径研究检索报告》审阅与修改指导意见——基于完整检索报告体例的完整性诊断、学术规范核查与方法论纠偏 上
数据库·ai·chatgpt·文心一言·检索·知网·豆包
泡海椒9 小时前
JQuick-SQL 快速开始:注册内存表并执行查询
数据库·windows·sql
ss27310 小时前
PG实战笔记 | 09 PostgreSQL psql客户端基本操作
数据库·笔记·postgresql
电商API_1800790524710 小时前
反向海淘多币种结算:汇率该在哪个时点被冻结
java·开发语言·数据库·跨境电商·1688 api