如何获取 MySQL 表中的字段数量?
要显示 MySQL 中的字段数量,请使用 COUNT(*)。以下为语法 -
select COUNT(*) AS anyAliasName from INFORMATION_SCHEMA.COLUMNS where table_name = yourTableName AND TABLE_SCHEMA = yourDatabaseName;
让我们先创建一个表 -
mysql> create table DemoTable794 ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int, ClientCountryName varchar(100) ); Query OK, 0 rows affected (1.61 sec)
以下是获取 MySQL 中字段数量的查询 -
mysql> select COUNT(*) AS TOTAL_NUMBER_OF_COLUMNS from INFORMATION_SCHEMA.COLUMNS where table_name = 'DemoTable794' AND TABLE_SCHEMA = 'web';
这将产生以下输出 -
+-------------------------+ | TOTAL_NUMBER_OF_COLUMNS | +-------------------------+ | 4 | +-------------------------+ 1 row in set (0.00 sec)
广告