
[英]Symfony2 Doctrine error: Cannot count query that uses a HAVING clause. Use the output walkers for pagination
[英]using operator in having clause. ERROR
set @mte := (select max(salary * months) from employee);
select count(name), salary*months
from employee
group by salary*months
having salary*months = @mte;
这会编译错误
第12行的错误1054(42S22):“具有子句”中的未知列“薪水” PS:很想使用Haven子句代替where。 我正在通过实验学习。 想知道为什么这不起作用PS:使用MYSQL LATEST
尝试在此处使用交叉应用,并用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;
通常,您在做什么应该没问题; 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;
最简单的解决方案不使用变量或子查询:
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认为需要对其进行汇总。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.