2016-05-27 2 views
1

私は基本的に私のmysqlテーブルのすべてのポイントを抽出するクエリを持っています。私は、Zend Frameworkの2を使用していますし、クエリは、基本的には、次のようになりますどのように多くの基本的事実に対応するた、すべての点は、ユーザーIDごとにグループ化されていることである今、私がやりたいものをZF2クエリで整数値を加算する

public function GetFiveUsersWithHighestPoints($brandId,$startDate, $endDate) 
    { 
     $select = new Select(); 
     $select->from(array("h" => $this->table)); 
     $select->columns(array(
      "id", 
      "userId", 
      "type", 
      "points", 
      "missionId", 
      "rewardId", 
      "brandId", 
      "goodsId", 
      "time", 
     )); 
     $select->order("h.points desc"); 
     $where = new Where(); 
     $select->join(array("m" => "missions"), "h.missionId = m.id", array("missionId" => "id", "missionName" => "title", "missionAction" => "action"), Select::JOIN_LEFT); 
     if (!empty($journeyId)) { 
      $where->equalTo("m.journeyId", $journeyId); 
     } else { 
      $select->join(array("r" => "rewards"), "h.rewardId = r.id", array("rewardId" => "id", "rewardName" => "name"), Select::JOIN_LEFT); 
     } 
     $where->notEqualTo("h.points", 0); 
     $where->equalTo("h.brandId", $brandId); 
     $where->between("h.time", $startDate, $endDate); 
     //分页代码块。固定用法 
     $select->where($where); 
     return $this->historyTable->selectWith($select)->toArray(); 
    } 

  1. ユーザーID 26 => 100ポイント
  2. ユーザーID 27:私はこのような何かを得るように、各ユーザーが指定した時間間隔で行われたポイントが...どのように私は、ユーザーIDごとのポイントのすべてを合計でくださいすることができます=> 200点

などなど

SUM()列を追加する方法について

答えて

0

どのように...私は、Zend Frameworkの2の書き込みクエリに非常に新たなんだと私は次に何をすることが出来るのですかわからないんだけどGROUP BY、のようなもの:

use Zend\Db\Sql\Expression; 

// Include a SUM() expression 
$select->cols([ 

    // Include existing columns 
    // etc... 

    // Add a SUM col 
    'pointsCount' => new Expression('SUM(h.points)'), 

]); 

// Add a GROUP BY 
$select->group('h.userId'); 

名前pointsCount下の各結果行で、次にアクセスすべき求めるポイントのユーザごとの合計。

関連する問題