Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count all records but only retrieve (LIMIT) a specific number for display?

I want to echo only the first 10 rows, but I need to count the total number of rows affected by the query.

I was doing a LIMIT 10 and then counting with the obvious problem I kept getting 10 as the count.

What would be a proper way to do it?

$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC");
$count = mysql_num_rows($data);

while($row = mysql_fetch_array( $data )) 
    { 
    echo $row['Site'];
    }
like image 733
lisovaccaro Avatar asked Nov 09 '11 04:11

lisovaccaro


People also ask

How do you get the total records even if limit is applied?

Since MYSQL 4.0 we can use SQL_CALC_FOUND_ROWS option in query which will tell MySQL to count total number of rows disregarding LIMIT clause. In main query add SQL_CALC_FOUND_ROWS option just after SELECT and in second query use FOUND_ROWS() function to get total number of rows without executing the query.

How do I limit the number of records in MySQL?

The MySQL LIMIT ClauseThe LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

Which clause in MySQL is used to display a fixed number of rows?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set.

How do I count a specific record in SQL?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How to select a limited number of Records in MySQL?

A MySQL supports the LIMIT clause to select a limited number of records. If we want to LIMIT the number of results that will return us a number of rows then we simply use the LIMIT command. Use the below SQL statement to create a database called geeks:

What is limit count in SQL query?

The LIMIT count is used with the SELECT statement to restrict the number of rows in the result set. Let’s understand this using some example queries. The LIMIT operator can be used in situations such as the above, where we need to find the top 2 participants having maximum percent and do not want to use any condition statements.

How to get the Count of a record in xrmtoolbox?

Run the report to get the count. Builder plugin to build the query, copy it. And use it in the FetchXML / View Record Counter plugin of XrmToolBox. Select the entity, contact, in this case, paste the FetchXML query and click Execute Count. We will get the count.

How to count the number of Records in a Dynamics 365 view?

With views, we are limited to just 5000 records. One option is to write the console app having the required QueryExpression or FetchXML condition using the Dynamics 365 SDK to get the count. We can also make use of SSRS reports here. In the LayOut fields window, specify Count as the summary type for grouping.


2 Answers

MySQL has some special support for this sort of thing. First, include SQL_CALC_FOUND_ROWS in your SELECT:

SELECT SQL_CALC_FOUND_ROWS *
FROM Badges
WHERE UID = '$user'
ORDER by Date DESC
LIMIT 10 -- Or whatever

Then pull out your rows and then immediately look at FOUND_ROWS() like this:

SELECT FOUND_ROWS()

to get the number of rows that matched your original query without considering the LIMIT clause.

This is MySQL-specific but it should be a little faster than doing two queries.

like image 52
mu is too short Avatar answered Sep 22 '22 04:09

mu is too short


It's pretty standard to issue two queries, one selecting the desired columns with the limit clause and another selecting only a count with no limit.

For example

$countQuery = 'SELECT COUNT(1) FROM Badges WHERE UID = ?';
$limitQuery = 'SELECT * FROM Badges WHERE UID = ? ORDER BY `Date` DESC LIMIT 0, 10';
like image 34
Phil Avatar answered Sep 24 '22 04:09

Phil