(十二)SQL | 数据查询语言DQL | 执行顺序


我们前几篇学习的是DQL的编写语句,那么DQL的执行顺序是什么呢?


执行顺序

  • 编写顺序(SFWGHOL):SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT …
  • 执行顺序(FWGHSOL):FROM … WHERE … GROUP BY … HAVING … SELECT … ORDER BY … LIMIT …

 接下来验证一下:

数据库:world(MySQL自带),数据表:city(MySQL自带)

-- where
select countrycode as cc, name as nm from city 
where name like 'a%'
order by countrycode;  -- 正常执行

select countrycode as cc, name as nm from city 
where nm like 'a%'  -- 换成别名
order by countrycode;  -- 报错 

-- group by
select countrycode as cc, avg(population) as avg_populaiton from city 
group by countrycode
order by countrycode;  -- 正常执行 

select countrycode as cc, avg(population) as avg_populaiton from city 
group by cc  -- 换成别名 
order by countrycode;  -- 报错

-- order by
select countrycode as cc, name as nm from city 
where name like 'a%'
order by cc;  -- 换成别名,正常执行
  1. where和group by使用别名时会报错!因为它们在select之前执行!order by使用别名则不会报错!
  2. Error Code: 1054. Unknown column 'nm' in 'where clause'
  3. MySQL中做了优化,group by后面使用别名,程序不会报错。

不总结=白学 


THE END


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值