获取行数
获取单个表的MySQL行计数
SELECT
COUNT(*)
FROM
table_name;SELECT
COUNT(*)
FROM
customers;+----------+
| COUNT(*) |
+----------+
| 122 |
+----------+
1 row in set (0.01 sec)Last updated
SELECT
COUNT(*)
FROM
table_name;SELECT
COUNT(*)
FROM
customers;+----------+
| COUNT(*) |
+----------+
| 122 |
+----------+
1 row in set (0.01 sec)Last updated
SELECT
'customers' tablename,
COUNT(*) rows
FROM
customers
UNION
SELECT
'orders' tablename,
COUNT(*) rows
FROM
orders;SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'yiibaidb'
AND table_type = 'BASE TABLE';+--------------+
| TABLE_NAME |
+--------------+
| customers |
| employees |
| offices |
| orderdetails |
| orders |
| payments |
| productlines |
| products |
+--------------+
8 rows in set (0.02 sec)SELECT
CONCAT(GROUP_CONCAT(CONCAT('SELECT \'',
table_name,
'\' table_name,COUNT(*) rows FROM ',
table_name)
SEPARATOR ' UNION '),
' ORDER BY table_name')
INTO @sql
FROM
table_list;SELECT
CONCAT(GROUP_CONCAT(CONCAT('SELECT \'',
table_name,
'\' table_name,COUNT(*) rows FROM ',
table_name)
SEPARATOR ' UNION '),
' ORDER BY table_name')
INTO @sql
FROM
(SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'yiibaidb'
AND table_type = 'BASE TABLE') table_list;WITH table_list AS (
SELECT
table_name
FROM information_schema.tables
WHERE table_schema = 'yiibaidb' AND
table_type = 'BASE TABLE'
)
SELECT CONCAT(
GROUP_CONCAT(CONCAT("SELECT '",table_name,"' table_name,COUNT(*) rows FROM ",table_name) SEPARATOR " UNION "),
' ORDER BY table_name'
)
INTO @sql
FROM table_list;USE yiibaidb;
PREPARE s FROM @sql;
EXECUTE s;
DEALLOCATE PREPARE s;+--------------+------+
| table_name | rows |
+--------------+------+
| customers | 122 |
| departments | 0 |
| employees | 23 |
| items | 9 |
| offices | 7 |
| orderdetails | 2998 |
| orders | 327 |
| payments | 273 |
| productlines | 7 |
| products | 110 |
| tasks | 0 |
| tokens | 1 |
+--------------+------+
12 rows in setSELECT
table_name,
table_rows
FROM
information_schema.tables
WHERE
table_schema = 'yiibaidb'
ORDER BY table_rows desc;mysql> SELECT
table_name,
table_rows
FROM
information_schema.tables
WHERE
table_schema = 'yiibaidb'
ORDER BY table_rows desc;
+--------------+------------+
| table_name | table_rows |
+--------------+------------+
| orderdetails | 2731 |
| orders | 326 |
| payments | 256 |
| customers | 122 |
| products | 110 |
| employees | 23 |
| items | 9 |
| productlines | 7 |
| offices | 7 |
| tokens | 0 |
| tasks | 0 |
| departments | 0 |
+--------------+------------+
12 rows in setANALYZE TABLE table_name, ...;