1

我有一个非常复杂的查询CSqlDataProvider,我试图让分页totalItemCount工作,但它返回错误的计数。我试图为 COUNT 个项目创建第二个 SQL,但这真的很难,有人可以帮助我吗?

CSqlDataProvider 文档:http ://www.yiiframework.com/doc/api/1.1/CSqlDataProvider

控制器代码:

    $dayofweek = date('w');     

    $sql = "( SELECT `profile`.`id` as profile_id, `profile`.`name` as profile_name, `events_reminder`.`id`,
                CASE WHEN weekday >= $dayofweek
                    THEN 
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
                    ELSE
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
                    END as revent_datetime,
                CASE WHEN weekday >= $dayofweek
                    THEN 
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
                    ELSE
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
                    END as rlist_datetime
                FROM events_reminder
                LEFT OUTER JOIN `profiles` `profile` ON (`events_reminder`.`profile_id`=`profile`.`id`)
                HAVING revent_datetime NOT IN (SELECT DATE_FORMAT(event_datetime, '%d/%m/%Y %H:%i') FROM events WHERE `events`.`profile_id`=`events_reminder`.`profile_id`) )
            UNION ALL
            ( SELECT `profile`.`id` as profile_id, `profile`.`name` as profile_name, `events_reminder`.`id`,
                CASE WHEN weekday >= $dayofweek
                    THEN 
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek + 7 DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
                    ELSE
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday + 7 DAY), ' ', event_time ), '%d/%m/%Y %H:%i')
                    END as revent_datetime,
                CASE WHEN weekday >= $dayofweek
                    THEN 
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL weekday - $dayofweek + 7 DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
                    ELSE
                        DATE_FORMAT( CONCAT( DATE_ADD(CURDATE(), INTERVAL 7 - $dayofweek + weekday + 7 DAY), ' ', list_time ), '%d/%m/%Y %H:%i')
                    END as rlist_datetime
                FROM events_reminder
                LEFT OUTER JOIN `profiles` `profile` ON (`events_reminder`.`profile_id`=`profile`.`id`)
                HAVING revent_datetime NOT IN (SELECT DATE_FORMAT(event_datetime, '%d/%m/%Y %H:%i') FROM events WHERE `events`.`profile_id`=`events_reminder`.`profile_id`) )
                ORDER BY revent_datetime";

    $count=Yii::app()->db->createCommand($sql)->queryScalar();

    $dataProvider=new CSqlDataProvider($sql, array(
        'totalItemCount'=>$count,
    ));

    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
4

1 回答 1

5

什么queryScalar是:

执行 SQL 语句并返回第一行数据中第一列的值。

因此,要使用它来获取count您必须在count(*)您的 sql 查询中添加一个,这也是在查询的开头,如下所示:

SELECT count(*), `profile`.`id` as profile_id, `profile`.`name` ...

更新:

由于您正在使用UNION您必须移动count(*)外部,还要给一个别名:

SELECT count(*) from (((select ...) UNION ALL (select ...)) as alias)

对于您的确切情况,您可以执行以下操作:

$count_sql='SELECT count(*) from (('. $sql .') as alias)';
$count=Yii::app()->db->createCommand($count_sql)->queryScalar();
于 2013-01-25T02:49:49.787 回答