gpt4 book ai didi

php - CSqlDataProvider 复杂查询和 totalItemCount

转载 作者:行者123 更新时间:2023-11-29 04:24:36 24 4
gpt4 key购买 nike

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

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

Controller 代码:

    $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,
));

最佳答案

什么 queryScalar做的是:

Executes the SQL statement and returns the value of the first column in the first row of data.

因此,要使用它来获取 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();

关于php - CSqlDataProvider 复杂查询和 totalItemCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com