php优化多更新选择查询

ddarikpa  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(334)

我有一个phpapi,在这里我检查不同的条件,并在此基础上获取行的计数并更新同一个表中的计数。
下面是我编写查询的条件:(注意:1个用户可以有多个活动)
1) 对于给定的用户(uid),对于type=impression的特定活动,我将获得impression计数并在表中更新相同的值
2) 对于给定的用户(uid),对于类型为action的特定活动,我将获取操作计数并在表中更新相同的计数

//To get the Impression/Action/Lead count
        $Impressionarr = [];
        $Actionarr = [];

//IMPRESSION COUNT
            $imp_qry = "select count(*) as ImpressionCount from ClicksAndImpressions where Uid = 101642 and CampaignID =100 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 = 101642 and CampaignID =100 ;";

            }else{
                echo '----not Present';
            }

//Impression count
              foreach($impData as $key=>$data)
              {
                  $data['ImpressionCount'];
                  $Impressionarr[] = $data; 
              }

//ACTION COUNT
            $action_qry = "select count(*) as ActionCount from ClicksAndImpressions where Uid = 101617 and CampaignID =81 and Type = 'Action';";
            $actionData = $this->getClicksAndImpressionsTable()->CustomQuery($action_qry);
            if($actionData[0]['ActionCount'] != '' || $actionData[0]['ActionCount'] !=NULL ){
                $action_update = "UPDATE ClicksAndImpressions SET ActionCount = ". $actionData[0]['ActionCount'] ." where Uid = 101617 and CampaignID =81 ;";
                $actionData = $this->getClicksAndImpressionsTable()->CustomUpdate($action_update); //print_r($actionData);exit;
            }else{
                echo '----not Present';
            }

//Action count
                foreach($actionData as $key=>$data)
                {
                    $data['ActionCount'];
                    $Actionarr[] = $data; 
                }

//Trying to combine the 3 arrays- but 2nd and 3rd not merging into 1st
$combine = array_merge($CampaignDetailsarr,$Impressionarr,$Actionarr);

1) 上面的重复-无论是在updatequery中还是在多个for循环中都可以避免。如果是这样的话,如何根据我的情况来优化上面的内容
ie)用于

if( Uid = 123 for some campaignId = 11 and if type is 'Impression')

      -> Update query for impression

else if(Uid = 123 for campaignId= 22 and if type = 'something else')

    -> Update query for something else

2) 我已经有一个数组1了。我需要将我的两个新数组(impressionarr[],actionarr[])合并到第一个数组中,但下面是我得到的结果。
预期:

作为:

mrphzbgm

mrphzbgm1#

通过在数据库中创建一个存储过程并用所需的数据调用它,您可能可以加快它的速度。据我所见,您必须执行的大部分逻辑都可以在存储过程中完成。

相关问题