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

推荐订阅源

量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
T
Threat Research - Cisco Blogs
C
Cisco Blogs
Recent Announcements
Recent Announcements
S
Securelist
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Project Zero
Project Zero
I
Intezer
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
T
Tenable Blog
Jina AI
Jina AI
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志

博客园 - 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'];