Home  >  Article  >  Database  >  Get the number of columns in a MySQL table?

Get the number of columns in a MySQL table?

PHPz
PHPzforward
2023-09-16 14:49:021091browse

Get the number of columns in a MySQL table?

To get the number of columns, use the aggregate function count(*) of the information_schema table in MySQL. The syntax is as follows to find the number of columns:

SELECT COUNT(*) as anyVariableName from INFORMATION_SCHEMA.COLUMNS where table_schema = ’yourDatabaseName’ and table_name = ’yourTableName’;

To understand the above syntax, let us create a table with some columns. Below is the query to create the table −

mysql> create table CountColumns
−> (
   −> Bookid int,
   −> BookName varchar(200),
   −> BookAuthorName varchar(200),
   −> BookPublishedDate datetime
−> );
Query OK, 0 rows affected (0.69 sec)

Now, I have a total of 4 columns in my table 'CountColumns'. You can use the above syntax to count the number of columns. The query is as follows:

mysql> SELECT COUNT(*) as NumberofColumns FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'business'
−> and table_name = 'CountColumns';

The output displays the count of the column −

+-----------------+
| NumberofColumns |
+-----------------+
| 4               |
+-----------------+
1 row in set (0.00 sec)

The above is the detailed content of Get the number of columns in a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete