简体   繁体   中英

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?

Eg. if I have a data table of 4 rows and two columns, I'd see something to that effect reported out. Perhaps [4,2] or something similar.

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. 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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