When you do a SHOW COLUMNS
or a DESCRIBE TABLE
, you're really just using the builtin special database called INFORMATION_SCHEMA
to pull information about a named table. Funny thing is, it seems to not return the information as a table, so it's impossible to get the data returned by those functions to act like a table (for sorting, subquerying, etc.).
Fortunately, you can set up your own query to perform the same lookup as SHOW or DESCRIBE:
select COLUMN_NAME as 'Field',
COLUMN_TYPE as 'Type',
IS_NULLABLE as 'Null',
COLUMN_KEY as 'Key',
COLUMN_DEFAULT as 'Default',
EXTRA as 'Extra'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'table' and
TABLE_SCHEMA = 'database'
-- add ordering --
order by Type;
Aside to OP: Thanks for giving me incentive to look up how to do this again. I once knew how to do this but had forgotten.