mysql官网语句执行顺序_Mysql 语句执行顺序

CODE_ico.pngico_fork.svg

select a.Customer

from orders a

where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘

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

82214be2159f5ce420358223be1e82fd.png

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

[html] view plaincopy

CODE_ico.pngico_fork.svg

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的字段来说是不重复的,结果如下:

49bed41d7a9bbca9bc65ed55a84eef18.png

语句三having:

[html] view plaincopy

CODE_ico.pngico_fork.svg

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,结果如下:

bb560bc92e1b353f8d56107b1bbf3b45.png

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

[html] view plaincopy

CODE_ico.pngico_fork.svg

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,将会是两条同样的记录):

f10c48ea2b90b799434bfaa919e5b73c.png

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

[html] view plaincopy

CODE_ico.pngico_fork.svg

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),结果如下:

45698e5f2fcb13aa33d8134fcc3bba2b.png

语句六order by:

[html] view plaincopy

CODE_ico.pngico_fork.svg

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

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

3b19b6faec0f13b6149b47e158574739.png

语句七limit:

[html] view plaincopy

CODE_ico.pngico_fork.svg

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条记录,结果如下:

dea2b443a4851baec8d4f37231f86f4a.png

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

[html] view plaincopy

CODE_ico.pngico_fork.svg

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 语句执行顺序

标签:tools   left join   学习   idt   strong   技术   ada   XML   code

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:http://www.cnblogs.com/lyftest/p/6400369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值