Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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 错误1111(HY000):组函数的使用无效_Mysql_Sql - Fatal编程技术网

Mysql 错误1111(HY000):组函数的使用无效

Mysql 错误1111(HY000):组函数的使用无效,mysql,sql,Mysql,Sql,给出下表 员工(ec、姓名、代码、dob、工资) 列出收入高于平均工资的员工 我的解决方案select*from staff where salary>avg(salary) 它有什么问题?聚合可能不会出现在WHERE子句中,除非它位于具有子句或选择列表的子查询中,并且被聚合的列是外部引用 使用WHERE子句的示例: select * from staff where salary > (select avg(salary) from staff) select deptid,COUN

给出下表 员工(ec、姓名、代码、dob、工资)

列出收入高于平均工资的员工

我的解决方案
select*from staff where salary>avg(salary)


它有什么问题?

聚合可能不会出现在
WHERE
子句中,除非它位于
具有
子句或选择列表的子查询中,并且被聚合的列是外部引用

使用
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
只能与
SELECT
语句一起使用
HAVING
通常用于
GROUP BY
子句中。当不使用
groupby
时,
HAVING
的行为类似于
WHERE
子句。

为什么会这样?为什么在where子句中可以使用count(*)?@jaig:在where子句中不能使用
count(*)
。请参阅我答案中的编辑。另一个疑问。。。为什么我不能执行以下操作:选择*来自有薪员工>平均(薪资),但我可以执行以下操作:选择*来自有薪员工>200@jaig:因为,它充当一个简单的where子句。请参阅我编辑的答案中的最后一部分。如果您满意,请随意接受答案。@TuğberkKaanDuman:当然,您可以在同一个查询中使用where和GroupBy。问题是在where子句中使用聚合函数。例如:在计数(病假)>30的薪资中,不能查询
SELECT*