简体   繁体   English

如何获取MySQL数据库中的总行数?

[英]How to get total number of rows in a MySQL database?

I have many tables in my MYSQL database. 我的MYSQL数据库中有很多表。 I want to be able to echo the total number of all the rows in a database. 我希望能够回显数据库中所有行的总数。

So basically I want to have it so the circled sum in the image below is echoed in PHP. 因此,基本上我想拥有它,以便在PHP中回显下图中的圆圈和。 在此处输入图片说明

This is my current code for just displaying the total rows from one table ($txid). 这是我当前的代码,用于仅显示一个表($ txid)的总行数。 I have tried replaced $txid with * but it doesnt work. 我试过用*代替$ txid,但是它不起作用。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks. 谢谢。

mysql_select_db($dbname);

$result = mysql_query("select count(1) FROM $txid");
$row = mysql_fetch_array($result);

$total = $row[0];
echo $total;

?>

Use the schema: 使用架构:

SELECT SUM(TABLE_ROWS) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = '{DB_NAME}'

That is what phpmyadmin is using. 那就是phpmyadmin使用的。

Source: Get record counts for all tables in MySQL database 来源: 获取MySQL数据库中所有表的记录计数

There is no way to "wildcard" across multiple tables - this is one reason why normalized [row-oriented] designs are good . 无法跨多个表“通配符”-这是归一化(面向行)设计良好的原因之一。

Instead, the query (or queries) must be written in such a way that that all the tables are specified manually - that is, there is a separate SELECT per table. 而是必须以一种方式编写查询(即一个或多个查询),以便手动指定所有表-也就是说, 每个表都有一个单独的SELECT。 These can then be grouped with UNION ALL and SUM'ed, or summed in PHP after running each query individually. 然后可以将它们与UNION ALL分组并加起来,或者在单独运行每个查询后在PHP中汇总。

select SUM(i) as total
from (
  select count(*) as i FROM tx
  union all
  select count(*) as i FROM tx2
  -- repeated for each table
) counts

(The SQL can be generated dynamically in PHP or in MySQL from a list of the tables to query.) (SQL可以在PHP或MySQL中从要查询的表列表中动态生成。)

If you only need a rough estimate , then the INFORMATION_SCHEMA TABLES.TABLE_ROWS table can be queried - and indeed this presents a row-oriented design . 如果您只需要粗略的估算 ,则可以查询INFORMATION_SCHEMA TABLES.TABLE_ROWS表-实际上,这提供了面向行的设计 However, at least for InnoDB tables, this is not guaranteed to return the same results as a direct COUNT. 但是,至少对于InnoDB表,这不能保证返回与直接COUNT相同的结果。

For InnoDB tables, the row count is only a rough estimate used in SQL optimization. 对于InnoDB表,行数只是SQL优化中使用的粗略估计。

Use the INFORMATION_SCHEMA database. 使用INFORMATION_SCHEMA数据库。 It's always a nice way to obtain meta data information: 这始终是获取元数据信息的好方法:

SELECT SUM(TABLE_ROWS)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = 'your_database';

And here's your updated PHP code: 这是您更新的PHP代码:

mysql_select_db($dbname);

$result = mysql_query("SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbname'");
$row = mysql_fetch_array($result);

$total = $row[0];
echo $total;

Read more about INFORMATION_SCHEMA.TABLES 阅读有关INFORMATION_SCHEMA.TABLES的更多信息

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

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