您可以使用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)