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

相关推荐
云边有个稻草人1 分钟前
金仓 VS MongoDB:国产数据库凭什么成为MongoDB平替首选?
数据库·mongodb·国产数据库·金仓·kingbasees sql
DarkAthena26 分钟前
【GaussDB】排查创建索引后查询数据行数发生变化的问题
数据库·sql·gaussdb
MengFly_27 分钟前
Compose 脚手架 Scaffold 完全指南
android·java·数据库
PPPPickup28 分钟前
application.yml或者yaml文件不显示绿色问题
java·数据库·spring
乔江seven28 分钟前
【python轻量级Web框架 Flask 】2 构建稳健 API:集成 MySQL 参数化查询与 DBUtils 连接池
前端·python·mysql·flask·web
面对疾风叭!哈撒给42 分钟前
Windows 系统安装 Mysql 8.0+
数据库·windows·mysql
he___H42 分钟前
Redis高级特性
数据库·redis·缓存
crossaspeed43 分钟前
Redis的持久化(八股)
数据库·redis·缓存
焦糖玛奇朵婷1 小时前
盲盒小程序开发科普:核心玩法与功能解析
大数据·数据库·程序人生·小程序·软件需求
市场部需要一个软件开发岗位1 小时前
数据仓库相关内容分享
数据库·数据仓库·oracle