Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:59:43+00:00 2026-06-17T19:59:43+00:00

I have a very complex query using CSqlDataProvider and I’m trying to get the

  • 0

I have a very complex query using CSqlDataProvider and I’m trying to get the pagination totalItemCount to work, but it’s returning wrong count. I tried to create a second SQL just to COUNT items, but it’s really hard, can anyone help me?

CSqlDataProvider Docs: http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider

Controller Code:

    $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,
    ));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:59:44+00:00Added an answer on June 17, 2026 at 7:59 pm

    What queryScalar does is:

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

    So to use it for getting count you will have to add a count(*) to your sql query, that too in the beginning of the query, like this:

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

    Update:

    Since you are using UNION you have to move the count(*) outside, also give an alias:

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

    For your exact situation you can do:

    $count_sql='SELECT count(*) from (('. $sql .') as alias)';
    $count=Yii::app()->db->createCommand($count_sql)->queryScalar();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very complex Linq to SQL query that returns a result set
i'm trying to do a complex query with Criteria in a Symfony project using
I have a rather complex logical query I'm trying to execute. Essentially there are
I have a very large and complex MySQL query that gets run from a
I have a query: (using a very simple example) SELECT ItemCategory, ItemID, Sales FROM
I have a very complex controller with a very complex set of views and
I have a very complex web app project I want to re-structure. Naturally, it
We have a very complex Oracle package here (over 4,100 lines of code) that's
I have a very complex Microsoft Access report. This report is run for multiple
I have a rather complex data source for my UITableView. I am very carefully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.