Mysql数据库查询未按预期工作
为什么我不能像这样写查询Mysql数据库查询未按预期工作,mysql,Mysql,为什么我不能像这样写查询 select * from STAFF having Salary > avg(salary); 在SQL中 PS:要找到薪水=平均薪水的员工,我需要写 select * from STAFF having Salary > (select avg(Salary) from STAFF); 还有其他方法吗?聚合可能不会出现在WHERE子句中,除非它位于HAVING子句或select列表中包含的子查询中,并且被聚合的列是外部引用 函数SUM()、A
select *
from STAFF
having Salary > avg(salary);
在SQL中
PS:要找到薪水=平均薪水的员工,我需要写
select *
from STAFF
having Salary > (select avg(Salary) from STAFF);
还有其他方法吗?聚合可能不会出现在
WHERE
子句中,除非它位于HAVING
子句或select列表中包含的子查询中,并且被聚合的列是外部引用
函数SUM()、AVG()、MIN()、MAX()、COUNT()等
称为聚合函数。阅读更多
使用WHERE
子句的示例:
select *
from staff
where salary > (select avg(salary) from staff)
select deptid,COUNT(*) as TotalCount
from staff
group by deptid
having count(*) >= 2
请参见中的示例
使用具有
子句的示例:
select *
from staff
where salary > (select avg(salary) from staff)
select deptid,COUNT(*) as TotalCount
from staff
group by deptid
having count(*) >= 2
请参见中的示例
哪里可以使用having子句:
Having
子句指定组或聚合的搜索条件HAVING
只能与SELECT
语句一起使用HAVING
通常用于GROUP BY
子句中。当未使用groupby
时,HAVING
的行为类似于WHERE
子句
阅读更多。@MarcusAdams你所说的循环是什么意思??