在我的查询中添加GROUP BY
时,Phalcon正在给我一个错误 - Phalcon \ Mvc \ Model \ Exception:语法错误,意外令牌GROUP,靠近' BY ...
这是我的代码;
$phql = "
SELECT $dist $ads_model.id AS id, $ads_model.title AS title, $ads_model.description AS description, $ads_model.country AS country, $ads_model.city AS city, $ads_model.latitude AS latitude, $ads_model.longitude AS longitude, $image_model.file AS image
FROM $ads_model
LEFT JOIN $image_model ON $image_model.ad_id = $ads_model.id
WHERE $where
GROUP BY $ads_model.id
$order
";
$ads = $this->modelsManager->executeQuery($phql);
这里是正在生成的查询;
Phalcon\Mvc\Model\Manager->executeQuery(\n SELECT (3959 * acos( cos( radians( xxxx ) ) * cos( radians( \Baseapp\Models\ClassifiedsAds.latitude ) ) * cos( radians( \Baseapp\Models\ClassifiedsAds.longitude ) - radians( xxxx ) ) + sin( radians( xxxx ) ) * sin( radians( \Baseapp\Models\ClassifiedsAds.latitude ) ) ) ) AS distance,
\Baseapp\Models\ClassifiedsAds.id AS id, \Baseapp\Models\ClassifiedsAds.title AS title, \Baseapp\Models\ClassifiedsAds.description AS description, \Baseapp\Models\ClassifiedsAds.country AS country, \Baseapp\Models\ClassifiedsAds.city AS city, \Baseapp\Models\ClassifiedsAds.latitude AS latitude, \Baseapp\Models\ClassifiedsAds.longitude AS longitude, \Baseapp\Models\ClassifiedsImages.file AS image\n
FROM \Baseapp\Models\ClassifiedsAds\n
LEFT JOIN \Baseapp\Models\ClassifiedsImages ON \Baseapp\Models\ClassifiedsImages.ad_id = \Baseapp\Models\ClassifiedsAds.id\n
WHERE \Baseapp\Models\ClassifiedsAds.status = 'active' Having distance <= 100\n
GROUP BY \Baseapp\Models\ClassifiedsAds.id\n
ORDER BY distance ASC\n )
在MySQL中运行查询的简化版本按预期工作。为什么Phalcon搞砸了GROUP BY
条款?如果省略GROUP BY
,则运行查询。如何让它在PHQL中运行?
另外,作为一个完全相关的问题:我正在使用PHQL,因为Phalcon的分页似乎与原始MySQL无关。有没有办法让Phalcon分页与raw一起工作(不进行自定义分页)?
答案 0 :(得分:1)
你的条款顺序错误。
您必须在HAVING
之后声明GROUP BY
条款。
[GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...]
有关完整示例,请参阅mysql docs。