MySQL数据库的DQL的高级数据查询语句
创始人
2024-12-14 13:36:37
0

目录

非等值联查:

等值联查:

eg:5张表联查

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

eg:

连接查询——union

Eg:

不去重的并集——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语句

1.简单case函数

2.case搜索函数

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

sql查询语句的执行顺序


目录

非等值联查:

等值联查:

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

eg:

连接查询——union

Eg:

不去重的并集——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 连接。

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

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

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

SELECT ... FROM就是调用SQL Interface

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

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

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

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

语法分析、语义分析。

5. 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、实时微信微乐小...
透视辅助!广西友乐免费辅助(辅... 透视辅助!广西友乐免费辅助(辅助挂)一贯真的是有挂(详细辅助必赢方法)1、广西友乐免费辅助透视辅助简...
透视辅助!好玩贰柒拾辅助(辅助... 透视辅助!好玩贰柒拾辅助(辅助挂)原来真的是有挂(详细辅助揭秘教程)1、好玩贰柒拾辅助ai辅助优化,...
透视辅助!丫丫陕西插件(辅助挂... 透视辅助!丫丫陕西插件(辅助挂)其实真的是有挂(详细辅助透视教程);1、每一步都需要思考,不同水平的...
透视辅助!途游游戏辅助脚本(辅... 透视辅助!途游游戏辅助脚本(辅助挂)都是存在有挂(详细辅助2025版教程);透视辅助!途游游戏辅助脚...
透视辅助!微信小程序蜀山四川血... 透视辅助!微信小程序蜀山四川血战辅助(辅助挂)本来真的有挂(详细辅助2025新版)1、下载好微信小程...
透视辅助!微信多乐跑得快辅助工... 透视辅助!微信多乐跑得快辅助工具(辅助挂)本来是真的有挂(详细辅助辅助教程)1、很好的工具软件,可以...
透视美元局!智星德州菠萝插件官... 透视美元局!智星德州菠萝插件官网,aapoker辅助挂,规律教程(有挂工具);智星德州菠萝插件官网辅...
透视辅助!闲聚辅助器(辅助挂)... 透视辅助!闲聚辅助器(辅助挂)果然是真的有挂(详细辅助技巧教程)所有人都在同一条线上,像星星一样排成...
透视黑科技!德普之星透视辅助软... 透视黑科技!德普之星透视辅助软件下载,aapoker脚本怎么用,黑科技教程(有挂细节)1、点击下载安...