获取MySQL表中的列数?
要获取列数,请对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 −> );
现在,我们的表'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)