我有一个像
这样的查询SELECT count(id),DATE_FORMAT(created,'%M') from searches group by month(created)
我得到的输出
count(id) DATE_FORMAT(created,'%M')
16 August
2 September
我在cakephp find方法中应用了这个查询,如下所示
$total = $this->Searches->find('count',[
'fields'=>['Searches.id',DATE_FORMAT('created','%M')],
'group' =>[month('Searches.created')]
]);
第一个我收到错误date_format()期望参数1.如何在cakephp find方法中应用我的顶级sql查询? 我正在使用cakephp 3版本。
答案 0 :(得分:1)
查询构建器有一个func()
方法,允许您使用SQL函数。
http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions
$time = $query->func()->date_format([
'created' => 'identifier',
"'%M'" => 'literal'
]);
然后,您可以在查询中使用该$time
变量。