Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you can solve your problem by adding a page number to your query. Unfortunately, this is moderately painful in mysql, because you have to use a self join or correlated subquery.</p> <p>Here is an example of how to do it with a correlated subquery:</p> <pre><code>select t.*, (select count(distinct order) from t t2 where t2.order &lt;= t.order) as pagenum from t order by pagenum </code></pre> <p>Based on your original query (but without putting it into a string):</p> <pre><code>with workouts as ( 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' AND `user_id` = ".$_session['user_id']." ) select wo.*, (select count(distinct order) from workouts wo2 where wo2.order &lt;= wo.order) as pagenum from workouts wo order by pagenum </code></pre> <p>This will almost work . . . just one caveat. You can put the "SELECT *" into the with clause because you have multiple columns with the same name. Put in the columns you need.</p> <p>I forget that mysql does not support the "with" statement. My apologies for the bad syntax. One work around is to use a view or temporary table. Otherwise, the query is a bit more complicated because the logic has to be repeated twice:</p> <pre><code>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' AND `user_id` = ".$_session['user_id']." ) wo2 where wo2.order &lt;= 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' AND `user_id` = ".$_session['user_id']." ) wo order by pagenum </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload