要获取列的数量,请使用MySQL中的information_schema表的聚合函数count(*)。语法如下,以查找列的数量:
SELECT COUNT(*) as anyVariableName from INFORMATION_SCHEMA.COLUMNS where table_schema = ’yourDatabaseName’ and table_name = ’yourTableName’;
为了理解上述语法,让我们创建一个带有一些列的表。以下是创建表的查询 −
mysql> create table CountColumns
−> (
−> Bookid int,
−> BookName varchar(200),
−> BookAuthorName varchar(200),
−> BookPublishedDate datetime
−> );
Query OK, 0 rows affected (0.69 sec)
现在,我的表格'CountColumns'中总共有4列。您可以使用上述语法来计算列的数量。查询如下:
mysql> SELECT COUNT(*) as NumberofColumns FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'business'
−> and table_name = 'CountColumns';
输出显示列的计数 −
+-----------------+
| NumberofColumns |
+-----------------+
| 4 |
+-----------------+
1 row in set (0.00 sec)