MySQL: Count number of columns in a table

February 6, 2012

Counting the number of columns, or fields, in a MySQL table is a fairly simple, though not exactly obvious task. You will need to know both the name of the table to query on, as well as the database. The query is as follows, plug in your values for database_name and table_name

SELECT COUNT(*)
       FROM INFORMATION_SCHEMA.COLUMNS
       WHERE table_schema = 'database_name'
             AND table_name = 'tbl_name'
Leave a Reply

Your email address will not be published. Required fields are marked *