SQL语句的执行顺序

时间:2021-06-02 15:53:24   收藏:0   阅读:0

SQL查询语句的基本结构

Select
.. 
from        
..
where       
..
group by    
..
having      
..
order by        
..

顺序:

  1. from
  2. where
  3. group by
  4. having
  5. order by
  6. select

正由此,在 where子句 中不能使用 分组函数(avg,max,min,sum,count)。必要的情况可以使用子查询代替
例如:

select ename,sal from emp where sal > avg(sal);   // 执行出错
select ename,sal from emp where sal > ( select avg(sal) from emp ); //使用子查询替代

注意: select 中出现的字段一定要出现在 group by中。否则返回的结果没有意义,没有出现在 gourp by 中的字段会从表中随机取数据。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!