目前我正在尝试为Kohana ORM构建另一个查询。这是我的SQL语句(对我来说很好)
SELECT needle, COUNT(needle) FROM my.haystacks GROUP BY needle ORDER BY COUNT(needle) DESC;
在Kohana中,它是以下片段:
$needleCount = ORM::factory('haystack')->select(array('needle'))->group_by('needle')->order_by('COUNT("needle")', 'DESC')->find_all()->as_array();
看起来不错?以下异常返回:
Unknown column 'COUNT("needle")'
[ SELECT `needle` AS `needle` FROM `haystacks` AS `haystack` GROUP BY `needle` ORDER BY `COUNT("needle")` DESC ]
因此,Kohana试图将COUNT(“针”)作为一列进行选择。有什么想法来解决这个问题吗?
答案 0 :(得分:0)
我的查询需要" DB :: expr(' COUNT(" needle")'"。所以用以下内容替换查询:
$needleCount = ORM::factory('haystack')->select(array('needle'))->group_by('needle')->order_by(DB::expr('COUNT("needle")'), 'DESC')->find_all()->as_array();