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 小时前
【9】PostgreSQL 之 vacuum 死元组清理
数据库·postgresql
风雅的远行者1 小时前
mysql互为主从失效,重新同步
数据库·mysql
晨岳1 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
宇钶宇夕2 小时前
S7-1200 系列 PLC 中 SCL 语言的 PEEK 和 POKE 指令使用详解
运维·服务器·数据库·程序人生·自动化
绿蚁新亭2 小时前
Spring的事务控制——学习历程
数据库·学习·spring
scilwb3 小时前
占用栅格地图数据集
数据库
时序数据说4 小时前
时序数据库的存储之道:从数据特性看技术要点
大数据·数据库·物联网·开源·时序数据库·iotdb
鸥梨菌Honevid5 小时前
QT解析文本框数据——概述
数据库·qt·mysql
今天又得骑车了5 小时前
一、MySQL 8.0 之《EXPLAIN ANALYZE 执行计划》
数据库·mysql·database
icecreamstorm5 小时前
MySQL 事务 最全入门
后端·mysql