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

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

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

相关推荐
AC赳赳老秦4 分钟前
电力能源公开数据采集实操:用 OpenClaw 合规抓取电网电价与发电量数据,生成区域能源供需分析报告
大数据·数据库·人工智能·python·php·deepseek·openclaw
云运维笔记6 分钟前
Zabbix 分布式监控搭建实战:基于 Proxy 实现 MySQL、Java、Nginx 监控
java·mysql·zabbix
StarRocks_labs14 分钟前
StarRocks 如何查询 Paimon 半结构化数据?Variant、Shredding 与 SQL 实践
starrocks·sql·paimon·variant·shredding
Discipline~Hai43 分钟前
Linux网络编程05-sqlite3数据库
linux·c语言·网络·数据库·sqlite·linux应用软件编程
青禾8371 小时前
一文入门 MySQL + MongoDB + Redis:三大数据库核心知识
数据库·mysql·mongodb
努力努力再努力wz1 小时前
【Redis入门系列】:从 RESP 协议到 redis-plus-plus:Redis 客户端编程与 C++ 接口设计
开发语言·数据库·c++·redis·分布式·缓存·架构
志栋智能1 小时前
凌晨3点的告警,如何用AI在5分钟内完成定界?
运维·服务器·数据库·人工智能·自动化
G.E.M.小白1 小时前
【SQLite3 数据库】从安装、SQL 操作到 C/C++ API 实战
数据库·oracle·sqlite3
神明不懂浪漫1 小时前
【第一章】MySQL简介
数据库·经验分享·笔记·mysql·oracle
山岚的运维笔记1 小时前
mysql 专业笔记 -- 第 14 章:GROUP BY
运维·数据库·笔记·后端·学习·mysql·dba