Nisto
Nisto

Reputation: 761

How do I limit an already limited query?

How can I limit a query that is already limited?

The thing is, I'm dividing the results into pages, and in my case, this is also done by LIMIT (is there really any other way to do it...?), so using LIMIT again seems to be impossible. Obviously, if you have any ideas on how this can be done in PHP instead (simple code, please), do post it.

Upvotes: 1

Views: 85

Answers (2)

Conrad Frix
Conrad Frix

Reputation: 52645

Use an Offset

e.g.

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

see the Select Documentation

The offset would be calculated by doing something like this

$offset = ($page-1) * $pagesize

The select would then be

SELECT 
     ...
   FROM
      yourtable
   LIMIT 
      $offset, $pagesize

Upvotes: 6

Shauna
Shauna

Reputation: 9596

What it sounds like you're looking for is pagination. Check out this related SO question for some good tutorial recommendations.

In short, you should be able to create a function that takes a limit and an offset and build your query from that.

Upvotes: 1

Related Questions