PHP Pagination MSSQL alternative to LIMIT? - php

To paginate a select query normally I would do this:
SELECT * FROM items WHERE condition = 1 LIMIT $start, $items
However, in MSSQL there is no LIMIT condition, so I tried with:
SELECT * FROM items WHERE condition = 1 OFFSET $start ROWS FETCH NEXT $items ROWS ONLY
And I get the error:
42000 - [SQL Server]Invalid usage of the option NEXT in the FETCH statement.
So what exactly should I do to paginate results?

This may help you.
The OFFSET-FETCH clause provides you with an option to fetch only a window or page of results from the result set. OFFSET-FETCH can be used only with the ORDER BY clause.
SELECT P_Name FROM ITEMS ORDER BY P_Name OFFSET 10 ROWS;
SRC: https://technet.microsoft.com/en-us/library/gg699618.aspx

The MS SQL equivalent to MySQL's LIMIT clause is TOP.
SELECT TOP 10 * FROM items WHERE condition = 1;
Will return the top ten rows, the same way it is done with LIMIT here:
SELECT * FROM items WHERE condition = 1 stuff LIMIT 10;
For more details (since I don't know all the context of your code) you can take a look here.

Related

Counting all rows with a limit

I need to filter (using where clauses) the rows in my table, count the total number of rows from this filter and then limit number of rows for pagination.
What is quickest/more efficient?
Count the rows with sql query. Select the rows with a limit with sql query.
Select all rows with sql query. Count the array with PHP. Split the array with PHP.
Or is there another way to count all rows and get a limited set of the results out?
You should use SQL_CALC_FOUND_ROWS and the FOUND_ROWS() function to return the total number of rows even when a LIMIT is applied to the returned results. After you run your query the results will be returned, and then you can run SELECT FOUND_ROWS() to return the total number of rows without having to run the query again.
SELECT SQL_CALC_FOUND_ROWS * FROM my_table WHERE column = 'something' LIMIT 10;
SELECT FOUND_ROWS() AS total_results;
Use two queries, one to retrieve total number of rows, another to get the rows. The first argument in LIMIT clause specifies the offset of the first row ( current_page * post_per_page ), and the second specifies the maximum number of rows to return ( post_per_page ).
SELECT COUNT(*) AS num_rows FROM table;
SELECT * FROM table LIMIT start, length;
The MySQL LIMIT should do the trick for you. This is a more efficient approach, as it helps in fetching only the relevant records to be displayed and nothing else.
Note that the startingIndex, numberOfRecordsPerPage variables should be set before executing the query.
SELECT * FROM MY_TABLE where
(...) //ALL CONDITIONS go here
LIMIT startingIndex, numberOfRecordsPerPage;
From the MySQL Reference
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
In order to identify whether or not to expect any records in return, one must first run the query (with COUNT and without any LIMIT condition):
`SELECT COUNT(*) FROM MY_TABLE where (...) //ALL CONDITIONS go here`
and store it separately. This shall be the total records applicable to the WHERE clauses given.
If the sum startingIndex+numberOfRecordsPerPage is >= TOTAL_COUNT then that paginated set shall be the last of the whole list and the user should not be allowed to click the NEXT button.
PS: Also, as pointed out in the comments, you might want to look at a framework alternative, like Criteria API for Java, to the heavy weightlifting, particularly if your WHERE conditions are also generated dynamically.

mysql select statement with limit offset bigger than actually records

I create an mysql-statement that paginates the results using LIMIT x, n (where x is the offset and n the returned entries).
The offset is created using GET-Vars in the form of page=x.
Now Google has some strange entries in its index that come from old crawls, where the page-variable exceeds the actual amount of records in the result-set.
Means, the query created with the page variable results in something like LIMIT 1000, 30 - but the query will only return 900 entries (since the content of the table changed meanwhile. This returns an empty result-set, of course.
Is there a way to tell mysql, that if the offset exceeds the returned records to just show the last possible result-set? I don't want to make an extra query using COUNT() first, since this would double the load on the mysql-server (right now I'm using SQL_CALC_FOUND_ROWS to determine the total amount of records the query would return without the LIMIT-Statement.
Get all requested rows, with at least one row (MySQL 5.6):
SELECT *
FROM `table`
LIMIT 1000, 30
UNION ALL (
SELECT *
FROM `table`
WHERE FOUND_ROWS() = 0
ORDER BY `id` DESC
LIMIT 1
)
SQL Fiddle

mysql query retrieval missing one row

i have retrieve data from database with this query:
SELECT *
FROM news, necat
WHERE news.ns_cat = necat.nc_id
ORDER BY ns_id DESC
LIMIT 0,4
and when this query run return only 3 row?!
can you find any problem?
LIMIT limits the number of possible results, but if your query only returns 3 results, it cannot extend it :)

Counting all results of MySQL query for paging

I am building a search page, each page featuring 10 results. For that purpose, I would like to display pages numbers at the bottom (like in Google for example) so a user can jump to a specific page result. In order to do that I need to know the overall count of the query (e.g., if I show 10 results per page and the specific query returns 73 rows in my Table, i would need to display links to 8 pages).
The only way I can think of is using the following (obviously inefficient) query:
// Query for the current page
$res = mysql_query("select * from TABLE WHERE COL='Sample' LIMIT $offset,10");
// Geeting the total count do I can build links to other pages
$res2 = mysql_query("select COUNT(*) fromTABLE WHERE COL='Sample'");
Is that the only way to do it?
Thanks,
Danny
you can use
SQL_CALC_FOUND_ROWS
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
SQL_CALC_FOUND_ROWS tells MySQL to
calculate how many rows there would be
in the result set, disregarding any
LIMIT clause. The number of rows can
then be retrieved with SELECT
FOUND_ROWS()

How to fetch the first 2 or 3 rows from Mysql DB using Mysql PHP function?

How to fetch the first two rows from Mysql DB using Mysql PHP function? Is there any function which can give me first 2 or 3 rows from the select query we fired?
Use LIMIT. From the manual, to retrieve 3 rows:
SELECT * FROM tbl LIMIT 3;
Or to retrieve rows 6-15:
SELECT * FROM tbl LIMIT 5,10;
For this query (i.e. with no constraint) if you are not using an ORDER BY clause your results will be ordered as they appear in the database.
You can use the limit clause in your query:
select * from your_table limit 3
This will select the first three rows.
And:
select * from your_table limit 5, 3
The later will select rows starting from 5 and return three rows.
You have 2 options:
Fire a query to select all the rows
and then select 2 or 3 rows as
needed using PHP.
Fire a query to select only the
necessary number of rows using LIMIT
clause.
The 2nd option is preferable.

Categories