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 :)
Related
I have to apply two queries for pagination.
1) Select all records.
2) Select records with limit.
As per example
$total_records = 'select * from table_name';
$for_pagination = 'select * from table_name limit 10, 2';
Is there any solution or way to get limit records and all records together with single query?
Can you use Union All? if they have the same number of columns and data type.
https://www.techonthenet.com/mysql/union_all.php
Otherwise, could you please provide further information (such as sample data) as we don't have much idea on your request based on information from your question.
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.
I want to paginate some result using
LIMIT no_of_rows, row_offset
query in mysql
I runt a script that does this via ajax, the problem is when I fetch the last rows.
How can I fetch the last rows via mysql limit without getting any errors?
The first time you query the table (assuming it does not change every second)
Do it like that:
SELECT SQL_CALC_FOUND_ROWS *
FROM table_name LIMIT page_size,0;
SELECT FOUND_ROWS();
That way you know how many rows to expect, and you do not give a LIMIT which does not exists.
I have a query that includes a LIMIT clause that returns say 10 results at a time out of a total of 100. What I want is also to count the total number of results (ie. 100). Do I have to do a second query without the LIMIT clause then count the number of rows returned? I dont want to do this as the query is quite expensive.
select sql_calc_found_rows * from table .... limit x;
select found_rows();
Take a look at the manual
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
that says:
If you are using SELECT SQL_CALC_FOUND_ROWS, MySQL must calculate how many rows are in the full result set. However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client.
Execute a second query on your mysql:
SELECT FOUND_ROWS();
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.