MySQL query / clause execution order

mysql的语句的执行顺序,因为今天和文佳在做查询的时候,有如下的sql:

 

select Xrow as newName

from Xtable

group by Xrow

having newName > 12;

 

很明显有一个问题 having 使用了select里面的语句,为毛呀,明明having的执行在select前面(我记得是)

 

百度一下

 

 

结论上面说了having是7 select是8 ,这太不可思议了~

 

没办法,不相信事实~

google it

http://stackoverflow.com/questions/24127932/mysql-query-clause-execution-order

 

The actual execution of SQL statements is a bit tricky. However, the standard does specify the order of interpretation of elements in the query. This is basically in the order that you specify, although I think having and group by could come after select:

  • FROM clause
  • WHERE clause
  • SELECT clause
  • GROUP BY clause
  • HAVING clause
  • ORDER BY clause

This is important for understanding how queries are parsed. You cannot use a column alias defined in a select in the where clause, for instance, because the where is parsed before the select. On the other hand, such an alias can be in the order by clause.

As for actual execution, that is really left up to the optimizer. For instance:

. . . group by a, b, c order by NULL

and

. . . group by a, b, c order by a, b, c

both have the effect of the "order by" not being executed at all -- and so not executed after the group by (in the first case, the effect is to remove sorting from the group by and in the second the effect is to do nothing more than the group by already does).

问题解决~看来查资料还是要看google

 

 





转载于:https://www.cnblogs.com/hexie/p/4989542.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值