Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql数据库查询未按预期工作_Mysql - Fatal编程技术网

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你所说的循环是什么意思??