Mysql 语句执行顺序

https://blog.csdn.net/jintao_ma/article/details/51253356

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

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

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

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

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

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

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

 

 
  1. select[distinct]

  2. from

  3. join(如left join)

  4. on

  5. where

  6. group by

  7. having

  8. union

  9. order by

  10. limit

 

 

 

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

 

 
  1. from

  2. on

  3. join

  4. where

  5. group by

  6. having

  7. select

  8. distinct

  9. union

  10. order by

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

 

建立如下表格orders:

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

语句一:

 

 
  1. select a.Customer

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams'

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

 


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

 

 
  1. select a.Customer,sum(a.OrderPrice)

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams'

  4. group by a.Customer

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

 

语句三having:

 

 
  1. select a.Customer,sum(a.OrderPrice)

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams'

  4. group by a.Customer

  5. having sum(a.OrderPrice) > 2000

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

 

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

 

 
  1. select distinct sum(a.OrderPrice)

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  4. group by a.Customer

  5. having sum(a.OrderPrice) > 1700

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

 

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

 

 
  1. select distinct sum(a.OrderPrice) As Order1

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  4. group by a.Customer

  5. having sum(a.OrderPrice) > 1500

  6. union

  7. select distinct sum(a.OrderPrice) As Order1

  8. from orders a

  9. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  10. group by a.Customer

  11. having sum(a.OrderPrice) > 2000

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

 

语句六order by:

 

 
  1. select distinct sum(a.OrderPrice) As order1

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  4. group by a.Customer

  5. having sum(a.OrderPrice) > 1500

  6. union

  7. select distinct sum(a.OrderPrice) As order1

  8. from orders a

  9. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  10. group by a.Customer

  11. having sum(a.OrderPrice) > 2000

  12. order by order1

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

 

语句七limit:

 

 
  1. select distinct sum(a.OrderPrice) As order1

  2. from orders a

  3. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  4. group by a.Customer

  5. having sum(a.OrderPrice) > 1500

  6. union

  7. select distinct sum(a.OrderPrice) As order1

  8. from orders a

  9. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  10. group by a.Customer

  11. having sum(a.OrderPrice) > 2000

  12. order by order1

  13. limit 1

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

 


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

 

 
  1. select distinct sum(a.OrderPrice) As order1,sum(d.OrderPrice) As order2

  2. from orders a

  3. left join (select c.* from Orders c) d

  4. on a.O_Id = d.O_Id

  5. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  6. group by a.Customer

  7. having sum(a.OrderPrice) > 1500

  8. union

  9. select distinct sum(a.OrderPrice) As order1,sum(e.OrderPrice) As order2

  10. from orders a

  11. left join (select c.* from Orders c) e

  12. on a.O_Id = e.O_Id

  13. where a.Customer='Bush' or a.Customer = 'Adams' or a.Customer = 'Carter'

  14. group by a.Customer

  15. having sum(a.OrderPrice) > 2000

  16. order by order1

  17. limit 1

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

 

注:

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

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

下篇讲如何进行多表查询

--------------------- 作者:Jintao_Ma 来源:CSDN 原文:https://blog.csdn.net/jintao_ma/article/details/51253356?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值