Get number of fields in MySQL table?

MySQLMySQLi Database

<p>To display number of fields in MySQL, use the COUNT(*). Following is the syntax &minus;</p><pre class="result notranslate">select COUNT(*) AS anyAliasName from INFORMATION_SCHEMA.COLUMNS where table_name = yourTableName AND TABLE_SCHEMA = yourDatabaseName;</pre><p>Let us first create a table &minus;</p><pre class="result notranslate">mysql&gt; 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)</pre><p>Following is the query to get the number of fields in MySQL &minus;</p><pre class="prettyprint notranslate">mysql&gt; select COUNT(*) AS TOTAL_NUMBER_OF_COLUMNS from INFORMATION_SCHEMA.COLUMNS where table_name = &#39;DemoTable794&#39; AND TABLE_SCHEMA = &#39;web&#39;;</pre><p>This will produce the following output -</p><pre class="result notranslate">+-------------------------+ | TOTAL_NUMBER_OF_COLUMNS | +-------------------------+ | 4 | +-------------------------+ 1 row in set (0.00 sec)</pre>
raja
Updated on 09-Sep-2019 08:49:46

Advertisements