groupBy([])不起作用

时间:2014-07-09 10:07:22

标签: php postgresql yii2

它没有给我任何错误,但结果没有分组。

public function getNumberOfRetweetsOfMeByDate($fromDate, $toDate, $twId)
{
     return $this->find()->asArray()
         ->select(["to_char (insert_time, 'YYYY-MM-DD') as time", "count(*) as number_of_retweets_of_me"])
         ->where("twitter_id = $twId")
         ->andWhere("insert_time >= '$fromDate'::DATE")
         ->andWhere("insert_time < '$toDate'::DATE +1")
         ->groupBy(['insert_time'])
         ->having('count(*) > 0')
         ->orderBy('insert_time')
         ->all();
 }

1 个答案:

答案 0 :(得分:1)

在groupBy和orderBy子句中使用别名time而不是insert_time

$this->find()
     ->asArray()
     ->select(["TO_CHAR(insert_time, 'YYYY-MM-DD') as char_time", "COUNT(*) as number_of_retweets_of_me"])
     ->where("twitter_id = $twId")
     ->andWhere("insert_time >= '$fromDate'::DATE")
     ->andWhere("insert_time < '$toDate'::DATE +1")
     ->groupBy(['char_time'])
     ->having('COUNT(*) > 0')
     ->orderBy('char_time')
     ->all();

另一个提示:尝试避免使用mysql关键字作为字段名称或别名,这可能会在其他情况下导致问题。