我需要在 symfony 学说中获得 AVG 列表的 MAX
我获得平均值的学说查询如下:
SELECT p,AVG(p.pathTime) as avgtime
FROM ShopperAnalyticsEntityBundle:Path p
JOIN p.shopper sh JOIN p.floor f
JOIN f.store s
WHERE p.floor=".$floorId." ".$filter."
GROUP BY p.xPath, p.yPath
为了获得平均值列表的最大值,我将其修改为(在引用this之后):
SELECT MAX(avgtime) as maxtime
FROM (SELECT p,AVG(p.pathTime) as avgtime
FROM ShopperAnalyticsEntityBundle:Path p
JOIN p.shopper sh
JOIN p.floor f
JOIN f.store s
WHERE p.floor=".$floorId." ".$filter."
GROUP BY p.xPath, p.yPath)
我收到以下错误:
[Semantical Error] line 0, col 38 near '(SELECT p,AVG(p.pathTime)': Error: Class '(' is not defined
所以我切换到本机 SQL 以获得相同的结果:
$maxSQL = "SELECT MAX(t.avgtime) as maxtime FROM ( SELECT AVG(p.path_time) as avgtime FROM path p JOIN shopper sh JOIN floor f JOIN store s WHERE p.floor_id=".$floorId." ".$filter." GROUP BY p.x_path, p.y_path ) t ";
$connection = $em->getConnection();
$statement = $connection->prepare($maxSQL);
$statement->execute();
$results = $statement->fetchAll();
我得到了结果,但 SQL 需要 23.4457 秒才能运行
谁能在 Doctrine 中提出一个简单的方法来解决这个问题。