How do I count columns of a table

MysqlSql

Mysql Problem Overview


For example :

tbl_ifo

id | name  | age | gender 
----------------------------
1  | John  |  15 |  Male
2  | Maria |  18 |  Female
3  | Steph |  19 |  Female
4  | Jay   |  21 |  Male

How can I count the columns of this table using mysql?

Mysql Solutions


Solution 1 - Mysql

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'tbl_ifo'

Solution 2 - Mysql

I think you need also to specify the name of the database:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'SchemaNameHere'
  AND table_name = 'TableNameHere'

if you don't specify the name of your database, chances are it will count all columns as long as it matches the name of your table. For example, you have two database: DBaseA and DbaseB, In DBaseA, it has two tables: TabA(3 fields), TabB(4 fields). And in DBaseB, it has again two tables: TabA(4 fields), TabC(4 fields).

if you run this query:

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'TabA'

it will return 7 because there are two tables named TabA. But by adding another condition table_schema = 'SchemaNameHere':

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DBaseA'
  AND table_name = 'TabA'

then it will only return 3.

Solution 3 - Mysql

$cs = mysql_query("describe tbl_info");
$column_count = mysql_num_rows($cs);

Or just:

$column_count = mysql_num_rows(mysql_query("describe tbl_info"));

Solution 4 - Mysql

To count the columns of your table precisely, you can get form information_schema.columns with passing your desired Database(Schema) Name and Table Name.


Reference the following Code:

SELECT count(*)
FROM information_schema.columns
WHERE table_schema = 'myDB'  
AND table_name = 'table1';

Solution 5 - Mysql

I have a more general answer; but I believe it is useful for counting the columns for all tables in a DB:

SELECT table_name, count(*)
FROM information_schema.columns
GROUP BY table_name;

Solution 6 - Mysql

Simply use mysql_fetch_assoc and count the array using count() function

Solution 7 - Mysql

this query may help you

SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE 
TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'tbl_ifo'

Solution 8 - Mysql

I think you want to know the total entries count in a table! For that use this code..

SELECT count( * ) as Total_Entries FROM tbl_ifo;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrjmcbView Question on Stackoverflow
Solution 1 - MysqlswapneshView Answer on Stackoverflow
Solution 2 - MysqlJohn WooView Answer on Stackoverflow
Solution 3 - MysqlToivo EUView Answer on Stackoverflow
Solution 4 - MysqlAKZapView Answer on Stackoverflow
Solution 5 - MysqlDouglas.SesarView Answer on Stackoverflow
Solution 6 - MysqlRamandeep sohiView Answer on Stackoverflow
Solution 7 - MysqlvarshaView Answer on Stackoverflow
Solution 8 - MysqlTinu MathaiView Answer on Stackoverflow