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

目录

非等值联查:

等值联查:

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

  • 25
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值