Mysql语句-执行顺序_MySQL

PHP中文网
发布: 2016-05-27 13:45:12
原创
1863人浏览过

1.这样一个问题,作为一个开发人员需要掌握数据库的哪些东西? 在开发中涉及到数据库,基本上只用到了sql语句,如何写sql以及对其进行优化就比较重要,那些mysql的厚本书籍针对的是dba,我们只需要学习其中的sql就可以了。

2.既然会写sql是目标,那么怎么才能写好sql.学习下面几点:

1)Mysql的执行顺序,这个是写sql的核心,之前遇到的一些错误就是因为对其不了解;

2)如何进行多表查询,优化,这个是很重要的部分;

3)sql语句的函数,sql提供的函数方便了很多操作;

3.这篇对Mysql语句执行顺序的学习做了总结:

1)Mysql语法顺序,即当sql中存在下面的关键字时,它们要保持这样的顺序:

 

select[distinct]
from
join(如left join)
on
where
group by
having
union
order by
limit
登录后复制

 

<br/>
登录后复制

 

2)Mysql执行顺序,即在执行时sql按照下面的顺序进行执行:

 

from
on
join
where
group by
having
select
distinct
union
order by
登录后复制

3)针对上面的Mysql语法顺序和执行顺序,循序渐进进行学习:

 

建立如下表格orders:

\

注:下面所有语句符合语法顺序(也不可能不符合,因为会报错^_^),只分析其执行顺序:(join和on属于多表查询,放在最后展示)

语句一:

 

select a.Customer
from orders a
where a.Customer='Bush' or a.Customer = 'Adams'
登录后复制

分析一:首先是from语句找到表格,然后根据where得到符合条件的记录,最后select出需要的字段,结果如下:

 

\
语句二groupby:groupby要和聚合函数一起使用

 

select a.Customer,sum(a.OrderPrice)
from orders a
where a.Customer='Bush' or a.Customer = 'Adams'
group by a.Customer
登录后复制

分析二:在from,where执行后,执行group by,同时也根据group by的字段,执行sum这个聚合函数。这样的话得到的记录对group by的字段来说是不重复的,结果如下:
\

 

语句三having:

 

select a.Customer,sum(a.OrderPrice)
from orders a
where a.Customer='Bush' or a.Customer = 'Adams'
group by a.Customer
having sum(a.OrderPrice) > 2000
登录后复制

分析三:由于where是在group之前执行,那么如何对group by的结果进行筛选,就用到了having,结果如下:

 

\

Python操作Mysql实例代码教程
Python操作Mysql实例代码教程

本文介绍了Python操作MYSQL、执行SQL语句、获取结果集、遍历结果集、取得某个字段、获取表字段名、将图片插入数据库、执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐。如果想查看在线版请访问:https://www.jb51.net/article/34102.htm

Python操作Mysql实例代码教程 0
查看详情 Python操作Mysql实例代码教程

语句四distinct: (为测试,先把数据库中Adams那条记录的OrderPrice改为3000)

 

select distinct sum(a.OrderPrice)
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 1700
登录后复制

分析四:将得到一条记录(没有distinct,将会是两条同样的记录):

 

\

语句五union:完全是对select的结果进行合并(默认去掉重复的记录):

 

select distinct sum(a.OrderPrice) As Order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As Order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 2000
登录后复制

分析五:默认去掉重复记录(想保留重复记录使用union all),结果如下:

 

\

语句六order by:

 

select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
登录后复制

分析:升序排序,结果如下:

 

\

语句七limit:

 

select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1
from orders a
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
limit 1
登录后复制

分析七:取出结果中的前1条记录,结果如下:

 

\
语句八(上面基本讲完,下面是join 和 on):

 

select distinct sum(a.OrderPrice) As order1,sum(d.OrderPrice) As order2
from orders a
left join (select c.* from Orders c) d 
on a.O_Id = d.O_Id
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 1500
union
select distinct sum(a.OrderPrice) As order1,sum(e.OrderPrice) As order2
from orders a
left join (select c.* from Orders c) e 
on a.O_Id = e.O_Id
where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'
group by a.Customer
having sum(a.OrderPrice) > 2000
order by order1
limit 1
登录后复制

分析八:上述语句其实join on就是多连接了一张表,而且是两张一样的表,都是Orders。 执行过程是,在执行from关键字之后根据on指定的条件,把left join指定的表格数据附在from指定的表格后面,然后再执行where字句。

 

注:

1)使用distinct要写在所有要查询字段的前面,后面有几个字段,就代表修饰几个字段,而不是紧随distinct的字段;

2)group by执行后(有聚合函数),group by后面的字段在结果中一定是唯一的,也就不需要针对这个字段用distinct;

 

以上就是Mysql语句-执行顺序_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号