2

I am using the datatables jquery plugin.

I would like to pull just the records needed for display with each query to the database, because there will possibly be 100s of thousands of records in the table. So instead of doing something like this and calling every record in the table and only showing a certain number of them at a time, due to pagination...

<cfquery name="get_users" datasource="dsn">
    select user_id, first_name, last_name
    from users
</cfquery>

<cfloop query="get_users" startrow="#startrow#" endrow="#endrow#">
    ...
</cfloop>

Is there a way to put the startrow and endrow within the cfquery tag or within sql somehow, to only get a certain number of records each time?

1 Answer 1

7

You'll want to send your starting point and number of records per "page" to the database, and have it return just those records. I don't know what database you're using, but here's an example of a query for MS SQL Server (2005+):

SELECT user_id, first_name, last_name
FROM (
  SELECT ROW_NUMBER() OVER(
    ORDER BY last_name, first_name 
  ) AS rownum, user_id, first_name, last_name
  FROM users
) AS users_page
WHERE rownum >= 1000 AND rownum <= 1010
ORDER BY last_name, first_name

This will give you the page of records from 1000 to 1010.

Here's the mysql version:

SELECT user_id, first_name, last_name
FROM users
ORDER BY last_name, first_name
LIMIT 999, 10 /* offset is zero-indexed in mysql */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.