2016-05-22 15:29:55 4329次浏览 3条回答 0 悬赏 10 金钱

--控制器

$connection = Yii::$app->db;

    $sql = "SELECT user_id,SUM(consumption) as consumption,SUM(achievement) 
           as achievement,SUM(total_bonus) as total_bonus,SUM(fudao_prize)
           as fudao_prize,SUM(a_bonus) as a_bonus,SUM(income) as income
           FROM ol_bonus GROUP BY user_id";
    $command = $connection->createCommand($sql)->queryAll();
    
    $pages = new Pagination(['totalCount' => count($command)]);
    $list = $connection->createCommand($sql." limit ".$pages->limit." offset ".$pages->offset."")->queryAll();
    $dataprovider = new ArrayDataProvider([
        'allModels' => $list,
    ]);
 
    return $this->render('check',[
        'bonus' => $dataprovider,
        'pages' => $pages,
    ]);

--视图

<?php if (isset($bonus)):?>

  <?php foreach ($bonus as $key=>$ad): ?>
      <tr>
        <td><?=$ad['user_id'] ?></td>
        <td><?=$ad['consumption'] ?></td>
        <td><?=$ad['achievement'] ?></td>
        <td><?=$ad['total_bonus'] ?></td>
        <td><?=$ad['fudao_prize'] ?></td>
        <td><?=$ad['a_bonus'] ?></td>
        <td><?=$ad['income'] ?></td>
   <?php endforeach; ?>

<?php endif; ?>

<?php

echo LinkPager::widget([
     'pagination' => $pages,
]);

?>
  • 回答于 2016-05-23 12:28 举报
    $sql = "SELECT user_id,SUM(consumption) as consumption,SUM(achievement) 
               as achievement,SUM(total_bonus) as total_bonus,SUM(fudao_prize)
               as fudao_prize,SUM(a_bonus) as a_bonus,SUM(income) as income
               FROM ol_bonus GROUP BY user_id";
    $command = $connection->createCommand($sql)->queryAll();
    ...count($command)...
    

    这里把$sql改成$sql = "select count(distinct user_id) from ol_bonus"更好吧

    2 条回复
    回复于 2016-05-24 08:22 回复

    这样只能取数据的个数,不能取数据的总和。

    回复于 2016-05-24 09:29 回复

    嗯,是的。你这段代码执行了两个查询分别是

    $command = $connection->createCommand($sql)->queryAll();
    

    $list = $connection->createCommand($sql." limit ".$pages->limit." offset ".$pages->offset."")->queryAll();
    

    第一句只是查询了个数,不需要将所有数据查出来再计数的。第二句的$sql不变即可。

  • 回答于 2016-08-30 18:24 举报

    这么写个人认为效率是不是有点低啊?如果数据多了..

  • 回答于 2017-01-05 17:09 举报


    **$dataprovider = new ArrayDataProvider([

        'allModels' => $list,
    ]); **
    

    这个没用

您需要登录后才可以回答。登录 | 立即注册
俗已入骨
试用期

俗已入骨

注册时间:2016-05-22
最后登录:2017-03-31
在线时长:2小时25分
  • 粉丝1
  • 金钱20
  • 威望0
  • 积分40

热门问题