惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - bwteacher

mysql操作错误 fastadmin操作 fastadmin下的多级联动 CentOS7安装Python3.10环境 表单提交fastadmin form jdk-24及tomcat-11.0.9(解压版)安装配置 mermaid基于 JavaScript 的图表绘制工具 HbuilderX 小随笔 v-bind和v-model的区别 v-if和v-show的区别 uniapp中@tap与@click点击事件的差异 PHP常用数学函数 距离-有这么多类 bootstrap.table汇总 selectpage汇总 jQuery没有匹配到任何内容,返回什么? js数组常用方法 thinkphp控制器名称命名相关 php常见错误码 ThinkPHP的join关联查询不使用默认的表前缀 阿里云虚拟主机创建https
mysql实用
bwteacher · 2026-04-27 · via 博客园 - bwteacher

一、获取l数据库中所有表的记录数

方法一:使用原生 SQL 查询 information_schema(推荐)

这是最高效的方法,因为它不需要先获取表名再循环查询,而是直接查询数据库的元数据。

注意:此方法获取的 TABLE_ROWS近似值(对于 InnoDB 引擎),但在数据量巨大时性能极高。

1. 获取每个表的记录数
phpuse think\Db;

// 假设你的数据库配置名为 'database',请根据实际情况修改
$dbName = '你的数据库名'; 
// 查询所有表的名称和行数
$list = Db::query("SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = '$dbName' AND table_type = 'BASE TABLE'");
dump($list);

2. 获取所有表的总记录数(求和)

如果你只需要一个总的数字,可以使用 SUM 函数:

use think\Db;

$dbName = '你的数据库名';

// 计算所有表行数之和
$result = Db::query("SELECT SUM(table_rows) as total_count FROM information_schema.tables WHERE table_schema = '$dbName' AND table_type = 'BASE TABLE'");

// 输出结果
echo "数据库总记录数约为:" . $result[0]['total_count'];