MySQL数据库的DQL的高级数据查询语句

目录

非等值联查:

等值联查:

eg:5张表联查

[连接查询------left/right/inner join on](#连接查询——left/right/inner join on)

eg:

连接查询------union

Eg:

[不去重的并集------union all](#不去重的并集——union all)

子查询(内部查询)

1、where型子查询

2、from型子查询:

3、exists型子查询:

any,some,all子查询:

any/some子查询:

Eg:

all子查询:

eg:

流程控制函数,语句

结果集的控制语句

IF(expr1,expr2,expr3)

IFNULL(expr1,expr2)

[case when then end语句](#case when then end语句)

1.简单case函数

2.case搜索函数

sql语句在数据库中的执行流程

sql查询语句的执行顺序


目录

非等值联查:

等值联查:

[连接查询------left/right/inner join on](#连接查询——left/right/inner join on)

eg:

连接查询------union

Eg:

[不去重的并集------union all](#不去重的并集——union all)

子查询(内部查询)


非等值联查:

SELECT * FROM 表1,表2

等值联查:

select * from 表1,表2 where 表1.字段1=表2.字段2...

eg:5张表联查

select * from student,class,sc,course,teacher where student.classid = class.classid and student.sid = sc.sid and sc.cid = course.cid and course.tid = teacher.tid

连接查询------left/right/inner join on

语法:select * from 表1 left\right\inner 表2 on 条件

left join:从左表(表1)中返回所有的记录,即便在右(表2)中没有匹配的行。

right join:从右表(table_2)中返回所有的记录,即便在左(table_1)中没有匹配的行。

inner join:在表中至少一个匹配时,则返回记录。

eg:

所有学生的数据和对应的班级信息

-- left join on 左外联

select * from student left join class on student.classid = class.classid

-- right join on 右外联

select * from class right join student on student.classid = class.classid

连接查询------union

注意:

两个结果集的并集

去除重复行 和distinct关键字 一样

不同类型的字段是可以合并

不同列数量的结果集不允许合并

起别名给第一个结果集才有用

Eg:

获取没有班级的学生和没有学生的班级

select * from student

left join class on student.classid = class.classid

union

select * from student

right join class on student.classid = class.classid

不去重的并集------union all

select * from student

left join class on student.classid = class.classid

union all

select * from student

right join class on student.classid = class.classid

子查询(内部查询)

1、where型子查询
2、from型子查询

把内层的查询结果当成临时表,供外层sql再次查询。查询结果集可以当成表看待。临时表要使用一个别名

3、exists型子查询:

把外层sql的结果,拿到内层sql去测试,如果内层的sql成立,则该行取出。内层查询是exists后的查询(子查询子句有结果,父句执行,子句没结果,父句不执行)

select * from teacher where exists (select * from student where classid=10);

any,some,all子查询:
any/some子查询:

表示满足其中任意一个条件

假设any内部的查询语句返回的结果个数是三个,

如:result1,result2,result3,那么,

select ...from ... where a > any(...);

->相当于:

select ...from ... where a > result1 or a > result2 or a > result3;

some 是 any的别名,所以用法是一样的,可以替代使用

Eg:

select DISTINCT student.* from sc left join student on sc.sid=student.sid where student.classid=1 and score > any //some

(select min(score) from sc left join student on sc.Sid=student.sid where student.classid=2);

all子查询:

表示满足其中所有条件条件,ALL关键字与any关键字类似,只不过上面的or改成and。

eg:

select DISTINCT student.* from sc left join student on sc.sid=student.sid where student.classid=1 and score > all

(select max(score) from sc left join student on sc.Sid=student.sid where student.classid=2);

流程控制函数,语句

结果集的控制语句
IF(expr1,expr2,expr3)

-- expr1 条件

-- exper2 条件成立 显示数据

-- exper3 条件不成立,显示数据

select * from teacher; -- 1女 -- 0男

select tid,tname,if(tsex=1,'女','男')tsex,Tbirthday,temail,Tmoney from teacher;

IFNULL(expr1,expr2)

exper1 字段

exper2 当字段为null,默认值

select sid,sname,IFNULL(birthday,"没有生日,有丢丢可怜!")birthday,ssex from student;

case when then end语句

1.简单case函数

select tid,tname,

case tsex

when 0 then '男'

when 1 then '女'

else '保密'

end tsex,Tbirthday,temail,Tmoney from teacher;

2.case搜索函数

select tid,tname,

case

when tsex<1 then '男'

when tsex=1 then '女'

when tsex>1 then '保密'

end tsex,Tbirthday,temail,Tmoney from teacher;

sql语句在数据库中的执行流程

1.系统(客户端)访问 MySQL 服务器前,做

的第一件事就是建立 TCP 连接。

  1. Caches & Buffers: 查询缓存组件

  2. SQL Interface: SQL接口 接收用户的SQL命

令,并且返回用户需要查询的结果。比如

SELECT ... FROM就是调用SQL Interface

MySQL支持DML(数据操作语言)、DDL

(数据定义语言)、存储过程、视图、触发器、

自定 义函数等多种SQL语言接口

  1. Parser: 解析器:在解析器中对 SQL 语句进行

语法分析、语义分析。

  1. Optimizer: 查询优化器

6.存储引擎

7.文件系统

8.日志系统

sql查询语句的执行顺序

手写:select distinct from where group by having order by

机读:from on where group by having select distinct order by

相关推荐
Zfox_2 小时前
Redis:Hash数据类型
服务器·数据库·redis·缓存·微服务·哈希算法
陈丹阳(滁州学院)4 小时前
若依添加添加监听容器配置(删除键,键过期)
数据库·oracle
远方16095 小时前
14-Oracle 23ai Vector Search 向量索引和混合索引-实操
数据库·ai·oracle
GUIQU.6 小时前
【Oracle】数据仓库
数据库·oracle
恰薯条的屑海鸥6 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖6 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
曼汐 .6 小时前
数据库管理与高可用-MySQL高可用
数据库·mysql
MickeyCV7 小时前
使用Docker部署MySQL&Redis容器与常见命令
redis·mysql·docker·容器·wsl·镜像
2301_793102497 小时前
Linux——MySql数据库
linux·数据库
喵叔哟7 小时前
第4章:Cypher查询语言基础
数据库