Search code examples
mysqlsql-order-byexplain

sort describe table foo output by field alphabetically


I have a sizeable table that I'm actively working on and modifying columns of. It'd be handy if I could sort a describe table by Field name alphabetically to quickly find the definition for a column. The docs for mysql describe nor explain mention anything about sorting. How can I do this?


Solution

  • DESCRIBE and SHOW COLUMNS don't have an option to sort their results. You can read more about them here: https://dev.mysql.com/doc/refman/5.7/en/show-columns.html

    If you want a customized list of columns of a table, you can query the INFORMATION_SCHEMA.

    SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, IS_NULLABLE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA='mydatabase' AND TABLE_NAME='mytable';
    

    Read more about this table here: https://dev.mysql.com/doc/refman/5.7/en/columns-table.html