Get number of fields in MySQL table?


To display number of fields in MySQL, use the COUNT(*). Following is the syntax −

select COUNT(*) AS anyAliasName
from INFORMATION_SCHEMA.COLUMNS
where table_name = yourTableName AND
TABLE_SCHEMA = yourDatabaseName;

Let us first create a table −

mysql> create table DemoTable794 (
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(100),
   ClientAge int,
   ClientCountryName varchar(100)
);
Query OK, 0 rows affected (1.61 sec)

Following is the query to get the number of fields in MySQL −

mysql> select COUNT(*) AS TOTAL_NUMBER_OF_COLUMNS
   from INFORMATION_SCHEMA.COLUMNS
   where table_name = 'DemoTable794' AND
   TABLE_SCHEMA = 'web';

This will produce the following output -

+-------------------------+
| TOTAL_NUMBER_OF_COLUMNS |
+-------------------------+
| 4                       |
+-------------------------+
1 row in set (0.00 sec)
raja
Published on 09-Sep-2019 12:19:46
Advertisements