set @mte := (select max(salary * months) from employee);
select count(name), salary*months
from employee
group by salary*months
having salary*months = @mte;
这会编译错误
ERROR 1054(42S22)第12行:未知专栏'薪水'在...拥有 条款' P.S:Woud喜欢使用having子句而不是where。我通过实验学习。想知道为什么这不起作用 P.S:使用MYSQL LATEST
答案 0 :(得分:0)
尝试在此处使用交叉申请并将其替换为where子句:
set @mte := (select max(salary * months) from employee);
select count(name), SalMonth
from employee cross apply (select salary*months SalMonth)a
where SalMonth = @mte
group by SalMonth;
答案 1 :(得分:0)
通常,你在做什么应该没问题; HAVING应该能够对聚合结果和任何按字段分组的字段进行操作(至少在它们被分组时,在这种情况下为salary*months
)。似乎MySQL正试图从单个字段值重新计算该产品(在该阶段它不再具有访问权限。)
尝试别名salary*months
,并使用GROUP BY和HAVING中的别名。
......但正如在评论中所说,无论如何,这种情况应该在WHERE中完成;并且可以在一个查询中完成:
select count(name), salary*months AS some_product
from employee
WHERE salary*months = (select max(salary * months) from employee)
group by some_product;
答案 2 :(得分:0)
最简单的解决方案不使用变量或子查询:
select count(name), salary*months as tot
from employee
group by salary*months
order by tot desc
limit 1;
您还可以使用别名来修复您的版本:
select count(name), salary*months as tot
from employee
group by salary*months
having tot = @mte;
或者使用聚合函数:
having max(salary*months) = @mte;
我想你可能在MySQL中发现了一个解析错误。你的版本应该工作,但显然表达式被分解为列引用 - 而MySQL认为它们需要聚合。