【发布时间】:2014-10-31 06:37:49
【问题描述】:
我在哪里可以将聚合函数放在一个 SQL 查询中(在 select 子句、having 子句或其他子句中)。而在select语句的逻辑处理顺序中聚合函数的执行顺序是什么?
那里的帖子“Order Of Execution of the SQL query”没有涵盖函数的执行顺序。
【问题讨论】:
标签: mysql sql-server sql-function
我在哪里可以将聚合函数放在一个 SQL 查询中(在 select 子句、having 子句或其他子句中)。而在select语句的逻辑处理顺序中聚合函数的执行顺序是什么?
那里的帖子“Order Of Execution of the SQL query”没有涵盖函数的执行顺序。
【问题讨论】:
标签: mysql sql-server sql-function
1) 子查询可以放在sql语句的Where、from和middle之后。
//After Where
select * from orders where order_id in (select Oreder_id from OrderConfirmed)
// After From
Select * from (select * from table) a
//Middle of sql Statement
select id,(select Id from table1) form table2
2) You Can put Aggregation function in both select clause and having clause.
//In select
select count(employee) from employees group by employee_dept
//In having
select count(employee) from employees group by employee_dept having(max(salary)>2000)
3) order of execution of sql statement
1)From
2)Where
3)Group By
4)Having
5)aggregate Function
6)Select
【讨论】:
Having和Group By的顺序不同。
group by 在having 之前执行。