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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - bwteacher

fastadmin中前端表格中字段状态动态加载问题 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'];