Skip to content

Bug: Can't paginate query with group by #2776

Closed
@samsonasik

Description

@samsonasik

For example, I have the following SQL:

SELECT `album`.*, 
             COUNT(track.id) as total_song 
             FROM `album` 
                 LEFT JOIN 
            `track` 
                 ON `album`.`id` = `track`.`album_id` 
           GROUP BY `album`.`id`;

For example, I use model(Album::class)->paginate(), the pagination is using query to get total num rows:

SELECT COUNT(*) AS `numrows`
             FROM `album` 
                 LEFT JOIN 
            `track` 
               ON `album`.`id` = `track`.`album_id` 
            GROUP BY `album`.`id`

Which the numrows results multiple rows for getting total rows which make data total rows incorrect.

For workaround, I modify the query with non-group by query:

SELECT 
    *,
   (SELECT count(*) FROM `track` WHERE `album_id` = `album`.`id`) AS total_song 
   FROM `album` 

CodeIgniter 4 version
4.0.2

Expected behavior, and steps to reproduce if appropriate
Show correct total numrows

Context

  • OS: OSX
  • Web server Apache
  • PHP version 7.4.4

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions