如何在SQL中SELECT和COUNT行?

时间:2011-11-11 07:00:23

标签: php sql

如何在一个查询中获得结果并计算结果? (我正在使用PDO语句)

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

4 个答案:

答案 0 :(得分:3)

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

使用PDO:

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

答案 1 :(得分:1)

这会将记录计数放在每行的末尾。

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

你应该只执行一个查询......

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

你可以通过mysql_num_rows()获得总计数;功能..像这样......

$count = mysql_num_rows($res);