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 8389557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:45:37+00:00 2026-06-09T18:45:37+00:00

I have a query that joins 3 tables and returns the results sorted by

  • 0

I have a query that joins 3 tables and returns the results sorted by page number so I can view only one page at a time and its output through an array.

Here is my query

select wo.*,
   (select count(distinct order)
    from (SELECT *
          FROM `movements` LEFT JOIN
               `classes`
               ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
               `clients`
               ON `movements`.`class_id` = `clients`.`class_id`
          WHERE `classes`.`class_id` = '$class_id' 
         ) wo2
    where wo2.order <= wo.order
   ) as pagenum
from (SELECT *
  FROM `movements` LEFT JOIN
       `classes`
        ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
        `clients`
       ON `movements`.`class_id` = `clients`.`class_id`
   WHERE `classes`.`class_id` = '$class_id' 
  ) wo
order by pagenum

Now I want to be able to get the MAX(pagenum) so I can tell PHP when I have reached the end of the query. Is there a way to do something like this (I have tried but keep getting sql syntax errors)

SELECT MAX(`pagenum`) as `lastpage`

Or is there a better way to accomplish this?

Below is a screenshot of the results of this query. I currently have “next” and “previous” link that take the user from page to page when I reach the end of the query (page 5 in these results) I want to have PHP no longer give the option to go to next page. So I somehow have to get the value of the MAX(pagenum) of each query.

http://custommovement.com/help/woquery.png

  • 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-09T18:45:39+00:00Added an answer on June 9, 2026 at 6:45 pm

    You can get the maxpage num by including another correlated subquery:

    select wo.*,
           (select count(distinct order)
            from (SELECT *
                  FROM `movements` LEFT JOIN
                       `classes`
                       ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
                       `clients`
                       ON `movements`.`class_id` = `clients`.`class_id`
                       WHERE `classes`.`class_id` = '$class_id'
                 ) wo2
            where wo2.order <= wo.order
           ) as pagenum,
           (select count(distinct order)
            from (SELECT *
                  FROM `movements` LEFT JOIN
                       `classes`
                       ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
                       `clients`
                       ON `movements`.`class_id` = `clients`.`class_id`
                       WHERE `classes`.`class_id` = '$class_id'
                 ) wo2
           ) as maxpages          
     from (SELECT *
           FROM `movements` LEFT JOIN
                `classes`
                ON `movements`.`class_id` = `classes`.`class_id` LEFT JOIN
                `clients`
                ON `movements`.`class_id` = `clients`.`class_id`
           WHERE `classes`.`class_id` = '$class_id'
          ) wo
    order by pagenum 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query that joins a bunch of tables. I'd like to
I have this query that returns the results by ordering it by focus.name ASC.
I have a MS SQL query that joins multiple tables and an example of
I have SQL query which joins 3 tables, one being just a many-to-many connecting
I have a query that currently returns data with the following attributes: A number
I have a query that joins two tables. The join is done on several
I have a query that returns results related to items that match a specific
I have a query like the following that returns the correct number of rows
I have a mysql query that uses union to join multiple queries into one
I have a query that basically combines tables of actions and selects from them

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.