MySQL 计算表格行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1893424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:46:09  来源:igfitidea点击:

Count table rows

mysql

提问by DJ.

What is the MySQL command to retrieve the count of records in a table?

检索表中记录数的 MySQL 命令是什么?

回答by Gregory Pakosz

SELECT COUNT(*) FROM fooTable;

will count the number of rows in the table.

将计算表中的行数。

See the reference manual.

请参阅参考手册

回答by Nick

Because nobody mentioned it:

因为没有人提到它:

show table status;

lists all tables along with some additional information, including estimated rows for each table. This is what phpMyAdmin is using for its database page.

列出所有表以及一些附加信息,包括每个表的估计行数。这就是 phpMyAdmin 用于其数据库页面的内容。

This information is available in MySQL 4, probably in MySQL 3.23 too - long time prior information schema database.

此信息在 MySQL 4 中可用,可能在 MySQL 3.23 中也可用 - 很长时间以前的信息模式数据库。

UPDATE:

更新:

Because there was down-vote, I want to clarify that the number shown is estimated for InnoDB and TokuDBand it is absolutely correct for MyISAM and Aria (Maria) storage engines.

因为有反对票,我想澄清一下,显示的数字是为 InnoDB 和 TokuDB 估计的,对于 MyISAM 和 Aria (Maria) 存储引擎来说绝对正确。

Per the documentation:

根据文档

The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.

行数。一些存储引擎,例如 MyISAM,存储确切的计数。对于其他存储引擎,比如 InnoDB,这个值是一个近似值,可能与实际值相差 40% 到 50%。在这种情况下,请使用 SELECT COUNT(*) 获得准确的计数。

This also is fastest way to see the row count on MySQL, because query like:

这也是查看 MySQL 行数的最快方法,因为查询如下:

select count(*) from table;

Doing full table scan what could be very expensive operation that might take hours on large high load server. It also increase disk I/O.

进行全表扫描可能是非常昂贵的操作,在大型高负载服务器上可能需要数小时。它还增加了磁盘 I/O。

The same operation might block the table for inserts and updates - this happen only on exotic storage engines.

相同的操作可能会阻止表的插入和更新——这只会发生在外来存储引擎上。

InnoDB and TokuDB are OK with table lock, but need full table scan.

InnoDB 和 TokuDB 可以用表锁,但需要全表扫描。

回答by Santhosh Tangudu

We have another way to find out the number of rows in a table without running a selectquery on that table.

我们还有另一种方法可以在不对该表运行select查询的情况下找出表中的行数。

Every MySQL instance has information_schema database. If you run the following query, it will give complete details about the table including the approximatenumber of rows in that table.

每个 MySQL 实例都有 information_schema 数据库。如果您运行以下查询,它将提供有关该表的完整详细信息,包括该表中的大致行数。

select * from information_schema.TABLES where table_name = 'table_name'\G

回答by David Snabel-Caunt

Simply:

简单地:

SELECT COUNT(*) FROM `tablename`

回答by Adriaan Stander

select count(*) from YourTable

回答by Suanbit

Just do a

做一个

SELECT COUNT(*) FROM table;

You can specify conditions with a Where after that

您可以在之后的 Where 中指定条件

SELECT COUNT(*) FROM table WHERE eye_color='brown';

回答by Yuseferi

If you have several fields in your table and your table is huge, it's better DO NOT USE*because of it load all fields to memory and using the following will have better performance

如果您的表中有多个字段并且您的表很大,最好不要使用,*因为它将所有字段加载到内存中,使用以下将有更好的性能

SELECT COUNT(1) FROM fooTable;

回答by Mohammad Kanan

As mentioned by Santosh, I think this query is suitably fast, while not querying all the table.

正如Santosh所提到的,我认为这个查询相当快,而不是查询所有表。

To return integer resultof number of data records, for a specific tablenamein a particular database:

要返回数据记录数的整数结果,对于特定数据库中的特定表名

select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = 'database' 
AND table_name='tablename';

回答by alpc

$sql="SELECT count(*) as toplam FROM wp_postmeta WHERE meta_key='ICERIK' AND post_id=".$id;
$total = 0;
$sqls = mysql_query($sql,$conn);
if ( $sqls ) {
    $total = mysql_result($sqls, 0);
};
echo "Total:".$total;`

回答by rashedcs

You have to use count() returns the number of rows that matches a specified criteria

您必须使用 count() 返回符合指定条件的行数

select count(*) from table_name;