如何在 MySQL 中选取列数最多的表?
mysqlmysqli database
您可以使用 INFORMATION_SCHEMA.COLUMNS 获取列数最多的表。
语法如下 −
SELECT TABLE_NAME, COUNT(*) AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY yourAliasName DESC LIMIT 1;
以下是选择具有最多列数的表的查询。我们之所以得到这个结果,是因为我们将计数设置为 DESC 并使用了 GROUP BY TABLE_NAME −
mysql> SELECT TABLE_NAME, COUNT(*) as TOTAL_COUNT FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY TOTAL_COUNT DESC LIMIT 1;
这将产生以下输出 −
+-----------------------------------+-------------+ | TABLE_NAME | TOTAL_COUNT | +-----------------------------------+-------------+ | table_lock_waits_summary_by_table | 68 | +-----------------------------------+-------------+ 1 row in set (0.12 sec)
您还可以使用以下查询获取最少的列数。我们之所以得到这个结果,是因为我们将计数设置为 ASC 并使用了 GROUP BY TABLE_NAME −
mysql> SELECT TABLE_NAME, COUNT(*) as TOTAL_COUNT FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY TOTAL_COUNT ASC LIMIT 1;
这将产生以下输出 −
+-----------------------+-------------+ | TABLE_NAME | TOTAL_COUNT | +-----------------------+-------------+ | removenullrecordsdemo | 1 | +-----------------------+-------------+ 1 row in set (0.14 sec)