mysql查询 流程_Mysql Select查询执行流程介绍及实例

Mysql查询执行过程如下:

1、  查询缓存,判断sql语句是否完全匹配,再判断是否有权限,两个判断为假则到解析器解析语句,为真则提取数据结果返回给用户。

2、  解析器解析。解析器先词法分析,语法分析,检查错误比如引号有没闭合等,然后生成解析树。

3、  预处理。预处理解决解析器无法决解的语义,如检查表和列是否存在,别名是否有错,生成新的解析树。

4、  优化器做大量的优化操作。

5、  生成执行计划。

6、  查询执行引擎,负责调度引擎获取相应数据

7、  返回结果。

Mysql查询执行示意图:

c5edb9bb31128f0dcc670bef4f8128b1.png

Mysql查询语句举例

列出表pet所有的列:

select * from pet

列出指定的列:

select name, owner form pet

select * from pet where (birth>'1980' and species='dog') or species='bird'

对null的条件:

select * from pet where sex is not null

所有名字第四位是n的宠物信息是:

select * from pet where owner like '___n%'

所有主人名叫gwen或benny的宠物:

select * from pet where owner in ('gwen' , 'benny')

查询出生日期在90年代是宠物,相当与 >= and  <=:

select * from pet where birth between '1990' and '1999'

按主人姓名排序,相同的按宠物姓名倒序排列:

select * from pet order by owner, name desc

查询性别为公的宠物,按生日倒序排列:

select * from pet where sex='m' order by birth desc

列出养有宠物狗的人名:

select distinct owner from pet where species='dog'

用两种方法查询出所有狗和猫的名字、出生年份、出生月份:

select name, left(birth,4) as year, mid(birth, 6, 2) as month from pet

where species='dog' or species='cat'

select name, year(birth) as year, month(birth) as month from pet

where species in('dog','cat')

查询所有名字中存在字母'e'的人,将他们养的宠物按类别、年龄排序:

select name, species, birthfrom pet

where owner like '%e%'

order by species,birth desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值