
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Get the count of two table fields in a single MySQL query?
- Display all fields of a table in MySQL?
- Get the number of columns in a MySQL table?
- Get the number of rows in a particular table with MySQL
- Easiest way to get number of rows in a MySQL table?
- Get the fields in each constraint in MySQL
- How to get number of rows in a table without using count(*) MySQL query?
- How to apply Substring() for fields in MySQL to get part of string?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- Get Last Entry in a MySQL table?
- Count number of rows in each table in MySQL?
- How to SELECT fields from one table and INSERT to another in MySQL?
- How to get primary key of a table in MySQL?
- Get the second last row of a table in MySQL?
- Get table names using SELECT statement in MySQL?

Advertisements