supercoolville
supercoolville

Reputation: 9096

How do I SELECT and COUNT rows in SQL?

How do I get the results and count the results in one query? (Im using PDO statements)

SELECT * FROM table; // gets results
SELECT COUNT(*) FROM table; // counts results

Upvotes: 0

Views: 3094

Answers (4)

nickb
nickb

Reputation: 59699

$result = mysql_query( "SELECT * FROM table");
$count = mysql_num_rows( $result);

Using PDO:

$statement = $dbh->prepare('SELECT * FROM table');
$statement->execute();
$count = $statement->rowCount();

Upvotes: 3

Rukmi Patel
Rukmi Patel

Reputation: 2561

you should execute only one query...

$sql = SELECT * FROM table;
$res = mysql_query($sql);

you can have total count by mysql_num_rows(); function.. like this ..

$count = mysql_num_rows($res);

Upvotes: 0

phatfingers
phatfingers

Reputation: 10250

SELECT *, (select count(*) FROM table) ct FROM table

Upvotes: 1

Adam Wenger
Adam Wenger

Reputation: 17550

This will put the record count at the end of each row.

SELECT *
   , COUNT(1) OVER () AS RecordCount
FROM table;

Upvotes: 1

Related Questions