How do I count columns in a table
Asked Answered
K

8

55

For example :

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?

Kyoko answered 8/5, 2012 at 3:28 Comment(0)
T
90
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'tbl_ifo'
Trample answered 8/5, 2012 at 3:31 Comment(1)
@codeMan 's point is very necessary. If you have multiple DBs with same table name then his point is must.Healthy
A
53

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.

Asymmetry answered 8/5, 2012 at 3:39 Comment(2)
I think your 'DatabaseNameHere' should be replaced with 'SchemaNameHere'Permanent
Adding database name will provide more accurate results if you're running this in an environment where you might have multiple of the same app installed, thus multiple X tables are in the schema.Lying
S
7
$cs = mysql_query("describe tbl_info");
$column_count = mysql_num_rows($cs);

Or just:

$column_count = mysql_num_rows(mysql_query("describe tbl_info"));
Substratum answered 11/12, 2015 at 9:5 Comment(1)
this is very clever and flexibleLeinster
V
5

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';
Voncile answered 8/5, 2012 at 5:0 Comment(2)
your answer is just the same as johntotetwooKyoko
yes, because i can't submit my answer due to my connection. but after my submit, there are two answers and one is accepted already :DVoncile
F
1

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;
Freemanfreemartin answered 8/6, 2018 at 12:49 Comment(0)
A
0

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

Aguedaaguero answered 15/1, 2015 at 9:8 Comment(0)
I
0

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'
Imperious answered 15/1, 2015 at 9:38 Comment(0)
S
-1

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;

Serried answered 18/10, 2018 at 7:22 Comment(1)
This tells you how many ROWS not how many COLUMNS ...Peppercorn

© 2022 - 2024 — McMap. All rights reserved.