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

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

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

相关推荐
<小智>4 小时前
及时做APP开发实战(十二)-LazyForEach懒加载优化实践
数据库·鸿蒙
龙仔7254 小时前
人大金仓KingbaseES V8 手动单库备份&恢复操作笔记
数据库·笔记·oracle·人大金仓
宠友信息6 小时前
消息序号如何保证即时通讯源码聊天记录稳定加载
java·spring boot·redis·python·mysql·uni-app
Csvn6 小时前
📊 SQL 入门 Day 6:多表查询 — JOIN 的四种姿势
后端·sql
夏贰四7 小时前
数据库迁移怎样降低人力投入?数据库迁移怎么实现全量增量同步?
数据库·数据库迁移
汇智信科7 小时前
图谱、向量、关键词、SQL多路一体,FastKG垂域召回率100%拉满
网络·数据库·ai编程·汇智信科·hsim·fastclaw·汇智龙虾
笨蛋不要掉眼泪8 小时前
MySQL架构揭秘:慢查询日志详解
数据库·mysql·架构
pulinzt9 小时前
Tableau的基础使用
数据库·numpy
糖果店的幽灵9 小时前
【langgraph 从入门到精通graphApi 篇】LangGraphAPI 方式调用 - 初识与核心概念
数据库·人工智能·langgraph
TlSfoward10 小时前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php