Home > Database > Mysql Tutorial > body text

Get the number of columns in a MySQL table?

PHPz
Release: 2023-09-16 14:49:02
forward
1073 people have browsed it

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’;
Copy after login

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)
Copy after login

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';
Copy after login

The output displays the count of the column −

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!