- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the number of columns in a MySQL table?
To get the number of columns, use the aggregate function count(*) with information_schema table from 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. The following is the query to create a table −
mysql> create table CountColumns −> ( −> Bookid int, −> BookName varchar(200), −> BookAuthorName varchar(200), −> BookPublishedDate datetime −> ); Query OK, 0 rows affected (0.69 sec)
Now, we have total 4 columns in my table ‘CountColumns’. You can apply 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 columns −
+-----------------+ | NumberofColumns | +-----------------+ | 4 | +-----------------+ 1 row in set (0.00 sec)
- Related Articles
- How to find the number of columns in a MySQL table?
- Count the number of columns in a MySQL table with Java
- How to get the datatype of MySQL table columns?
- How to get the number of columns of a table using JDBC?
- Get number of fields in MySQL table?
- Counting the number of non-null or nonzero columns in a MySQL table?
- Get the number of rows in a particular table with MySQL
- How can we get a list of columns in an existing MySQL table?
- How to know the exact number of table and columns in a MySQL database?
- How to can I get the names of my MySQL table columns?
- How to select the table with the greatest number of columns in MySQL?
- Easiest way to get number of rows in a MySQL table?
- Do we have any other statement in MySQL instead of SHOW COLUMNS to get the list of columns in an existing table?
- Maximum number of columns in a table in SAP HANA
- Split float value in two columns of a MySQL table?

Advertisements