如何統計Mysql精確的記錄數

2021-10-08 07:24:58 字數 1098 閱讀 9371

對於innodb表,行數只是sql優化中使用的粗略估計,information_schema是不精確的,sum(table_rows) 和 count(1) 結果會不一樣。

# 1.資料庫查詢總條數

mysql>use information_schema;

mysql>select sum(table_rows) from tables where table_schema =

'test_db'

;# 2.mysql 檢視資料庫中所有表的記錄數

mysql>use information_schema;

mysql>select table_name,table_rows from tables where table_schema =

'test_db' order by table_rows desc;

2.1 快速生成統計sql
select concat(

'select "',

table_name,

'", count(*) from ',

table_schema,

'.',

table_name,

' union all'

) from information_schema.tables

where table_schema='test_db';

2.2 拼接匯出結果
-- 去掉最後的union all

select sum(`count(*)`) from (

select "tbl_aa", count(*) from test_db.tbl_aa union all

select "tbl_bb", count(*) from test_db.tbl_bb union all

select "tbl_cc", count(*) from test_db.tbl_cc union all

select "tbl_dd", count(*) from test_db.tbl_dd

) a ;

Mysql的統計重覆記錄

雖說sql語法,這些主流的資料庫引擎都支援,但是每乙個資料庫引擎都有自己的特性,例如統計並顯示非重複的資料。mysql的實現形式是 資料庫test id name 1 agle 2 blibli 3 cat 4 cat 5 blibli 比如我想用一條語句查詢得到name不重複的所有資料,那就必須使...

SQLSERVER中統計所有表的記錄數

sqlserver中統計所有表的記錄數 2009 06 25 17 07 36 分類 linux 曾經試著寫過乙個儲存過程,作用就是刪除所有表的內容,思路是這樣的 首先通過sysobjects表構造乙個sql語句字串 delete 表名 其中表名就是sysobjects中的name列,把這些dele...

MySQL統計函式記錄 時間段統計

按年彙總,統計 select sum mymoney as totalmoney,count as sheets from mytable group by date format col,y 按月彙總,統計 select sum mymoney as totalmoney,count as she...