Count table rows

后端 未结 11 1783
一个人的身影
一个人的身影 2020-11-28 21:55

What is the MySQL command to retrieve the count of records in a table?

相关标签:
11条回答
  • 2020-11-28 22:50

    We have another way to find out the number of rows in a table without running a select query on that table.

    Every MySQL instance has information_schema database. If you run the following query, it will give complete details about the table including the approximate number of rows in that table.

    select * from information_schema.TABLES where table_name = 'table_name'\G
    
    0 讨论(0)
  • Just do a

    SELECT COUNT(*) FROM table;
    

    You can specify conditions with a Where after that

    SELECT COUNT(*) FROM table WHERE eye_color='brown';
    
    0 讨论(0)
  • 2020-11-28 22:57

    If you have several fields in your table and your table is huge, it's better DO NOT USE * because of it load all fields to memory and using the following will have better performance

    SELECT COUNT(1) FROM fooTable;
    
    0 讨论(0)
  • 2020-11-28 23:00

    As mentioned by Santosh, I think this query is suitably fast, while not querying all the table.

    To return integer result of number of data records, for a specific tablename in a particular database:

    select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = 'database' 
    AND table_name='tablename';
    
    0 讨论(0)
  • 2020-11-28 23:00

    You have to use count() returns the number of rows that matches a specified criteria

    select count(*) from table_name;
    
    0 讨论(0)
提交回复
热议问题