简体   繁体   English

如何计算mysql中的行数

[英]How to count number of columns and rows in mysql

I have a requirement where i need to count number of columns, number of rows, and size of each table for a given database. 我有一个要求,我需要计算给定数据库的列数,行数和每个表的大小。

The query below is not giving the exact result, 以下查询未提供确切结果,

SELECT t1.table_name, t1. table_rows,COUNT(t2.table_name) AS table_colums, t1. data_length 
FROM INFORMATION_SCHEMA.TABLES t1 
JOIN (SELECT table_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='my_db_name') t2 
ON t1.table_name=t2.table_name 
WHERE t1.TABLE_SCHEMA = 'my_db_name';

Any clues? 有什么线索吗?

Counting rows: 计算行数:

SELECT COUNT(*) FROM yourtable;

Counting columns: 计数列:

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'yourtable'

If you mean by size of each table the size in MB, then: 如果用每个表的大小表示以MB为单位的大小,则:

SELECT 
    table_name AS `Table`, 
    round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "yourdatabase"
    AND table_name = "yourtable";

This query solved my problem. 这个查询解决了我的问题。

SELECT t1.table_name, t1.table_rows, 
(SELECT COUNT(*) 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'my_db_name'
AND table_name = t1.table_name) AS colsm, 
t1.data_length
FROM INFORMATION_SCHEMA.TABLES t1
WHERE t1.TABLE_SCHEMA = 'my_db_name';

This might get you the required result 这可能会为您提供所需的结果

SELECT t1.table_name
,round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
,(select count(*) from information_schema.columns c where c.table_schema = t1.table_schema and c.table_name = t1.table_name) as cols
,t1.table_rows as rows
FROM INFORMATION_SCHEMA.TABLES t1 
WHERE t1.table_schema = 'my_db_name'

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

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