迭代每一行并获取计数php mysql

时间:2018-12-01 08:31:04

标签: php mysql

我在mysql中有一行

Uid  campid  type
1    100     Type1
1    100     Type1
1    200     Type1

我需要获取每个 campid

的每个 Type1 的数量

ie) 对于Type1的Campid 100-计数= 2

对于Type1的Campid 200-计数= 1

下面,我得到了我不想要的所有Campid的总数。

$Impressionarr = [];
$imp_qry = "select count(*) as ImpressionCount from ClicksAndImpressions where Uid = 101655 and CampaignID = 109 and Type='Impression' ;";
$impData = $this->getClicksAndImpressionsTable()->CustomQuery($imp_qry);

if($impData[0]['ImpressionCount'] != '' || $impData[0]['ImpressionCount'] !=NULL ){
                $impr_update = "UPDATE ClicksAndImpressions SET ImpressionCount = ". $impData[0]['ImpressionCount'] ." where Uid = 101655 and CampaignID =109 and Type='Impression' ;";
                $impqryexecute= $this->getClicksAndImpressionsTable()->CustomUpdate($impr_update);

                foreach($impData as $key=>$data2)
                {
                    $data2['ImpressionCount'];
                    $Impressionarr[] = $data2; 
                }

            }else{
                return array("errstr" => "success.", "success" => 0, "Campaigns" => "No Impression Counts");
            }


public function CustomQuery($id) {
  $sql = $id;
  foreach (($this->tableGateway->getAdapter()->driver->getConnection()->execute($sql)) as $row){
        $results[] = $row;
    }
    return $results;
}

1 个答案:

答案 0 :(得分:1)

select count(uid) as total, type from ClicksAndImpressions  group by campid  

尝试上述查询