简体   繁体   English

计算 mysql 中的行数?

[英]count the number of rows in mysql?

How to count the number of rows in mysql database tables with php?如何用php统计mysql数据库表的行数?

if have in database 5 rows, they number show like this( bold ):如果在数据库中有 5 行,它们的编号显示如下(粗体):

all columns is: 5所有列是: 5

1 row1 1行1
2 row2 2行 2
3 row3 3排3
4 row4 4排4
5 row5 5排5

Just use this SQL query:只需使用此 SQL 查询:

SELECT COUNT(*) FROM table_name

If you want to know how many rows were returned by another query, you can use the PHP mysql_num_rows function.如果您想知道另一个查询返回了多少行,可以使用 PHP mysql_num_rows function。 (You could also just increment a counter for each row you process, but this function is handy if you need to know the number of records prior to enumerating the results.) (您也可以只为您处理的每一行增加一个计数器,但是如果您需要在枚举结果之前知道记录的数量,这个 function 很方便。)

How do we count them back together?我们如何将它们重新计算在一起? LIKE: 1 2 3 4 5. i not want use of id in column database LIKE: 1 2 3 4 5. 我不想在列数据库中使用 id

select list_of_fields,@rn:=@rn+1 as row_num
from table,(select @rn:=0) as r order by id

You can use this你可以用这个

$result = $this->db->get(<table_name>);
$num_rows = $result->num_rows();

$num_rows would be the total rows in table_name $num_rows将是table_name中的总行数

Then you can just do this然后你可以这样做

echo 'The number of rows in table_name is '.$num_rows ; echo 'The number of rows in table_name is '.$num_rows ;

This query should do the trick for you:此查询应该为您解决问题:

SELECT COUNT(*) as count FROM `table`

The result set would be结果集将是

[count]
-------
5

Assuming you have 5 rows.假设你有 5 行。


In order to manually count each row and display its index (without using ID), I would do something like为了手动计算每一行并显示其索引(不使用 ID),我会做类似的事情

$counter = 1;
$stmt = $db->prepare('SELECT `field` FROM `table`');
$stmt->execute();
while($row = $stmt->fetch()) {
    echo "<b>{$counter}:</b> {$row['field']}";
    $counter++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM