简体   繁体   English

计算mysql数据表中有多少行和列

[英]Count how many rows and columns are in a mysql datatable

I can't believe this hasn't been asked yet, but how do I get the shape of a datatable in a mysql database? 我不敢相信还没有问过这个问题,但是我如何在mysql数据库中获得数据表的形状呢?

Eg. 例如。 if I have a data table of 4 rows and two columns, I'd see something to that effect reported out. 如果我有一个包含4行和2列的数据表,我会看到有这种效果的报告。 Perhaps [4,2] or something similar. 也许[4,2]或类似的东西。

For the number of rows, a simple count query will do: 对于行数,将执行简单的计数查询:

SELECT COUNT(*) AS num_rows FROM yourTable;

For the number of columns, we need to do a bit more work: 对于列数,我们需要做更多的工作:

SELECT COUNT(*) AS num_columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'yourdatabasename' 
AND TABLE_NAME = 'yourTable';

You could combine these into a single query if you wanted, or even write a stored procedure. 您可以根据需要将它们组合成一个查询,甚至可以编写一个存储过程。 By the way, I can't even think of the last time I needed to know the number of columns in a MySQL table. 顺便说一句,我什至无法想到上一次我需要知道MySQL表中的列 Most of the time, you would know beforehand which columns you wanted to access, and so you would not need a numerical count. 大多数时候,您会事先知道要访问哪些列,因此您不需要数字计数。 The number of rows is another story, and often you might want to know this. 行数是另一回事,通常您可能想知道这一点。

For MySQL use the following query in your database : 对于MySQL,请在数据库中使用以下查询:

SELECT
    table_schema,
    table_name,
    data_length,
    index_length
FROM information_schema.tables
WHERE TABLE_NAME = 'tableName';

They are stored in the information schema which keeps details of all the tables created. 它们存储在信息架构中,该架构保留所有已创建表的详细信息。

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

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