mysql統計一張表中條目個數的方法,mysql條目

來源:互聯網
上載者:User

mysql統計一張表中條目個數的方法,mysql條目

統計一張表中條目的個通常的SQL語句是:

select count(*) from tableName;#orselect count(1) from tableName;#or 統計一個列項,如IDselect count(ID)

另外,可通過使用information_schema統計個數

MySQL中有一個名為 information_schema 的資料庫,在該庫中有一個 TABLES 表,這個表主要欄位分別是:

 

TABLE_SCHEMA : 資料庫名

TABLE_NAME:表名

ENGINE:所使用的儲存引擎

TABLES_ROWS:記錄數

DATA_LENGTH:資料大小

INDEX_LENGTH:索引大小

下面的SQL語句給出了查詢方法,同時也統計出了佔用儲存空間的資訊:

SELECT information_schema.`TABLES`.TABLE_NAME,       (DATA_LENGTH/1024/1024) as DataM ,    (INDEX_LENGTH/1024/1024) as IndexM,     ((DATA_LENGTH+INDEX_LENGTH)/1024/1024) as AllM,    TABLE_ROWSfrom information_schema.`TABLES` where information_schema.`TABLES`.TABLE_SCHEMA='abc';

mysql中統計一張表中,不同status的行的個數

select status,count(*) from table group by status
 
一個統計資料庫 所有表的記錄條數的總與的語句(mysql)

<?php
mysql_connect('localhost','資料庫帳號','資料庫密碼');
mysql_select_db('資料庫名');

$result = mysql_query('show tables'); //擷取所有表名

$all_record = 0;
while($record = mysql_fetch_row($result)) {
$tb_name = $record[0]; //表名

$n = mysql_result(mysql_query('select count(*) from `'.$tb_name.'`'),0);
$all_record+=$n;
}

echo $all_record; //總記錄數

?>
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.