Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that
SHOW COLUMNS `table_name`;
or
DESCRIBE `table_name`;
will give me a list of the columns in a table (along with other info), but is it possible to alter the query in order to get the columns sorted alphabetically. Adding ORDER BY 'Field' didn't work, it gave a syntax error.
SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('Your_Table_Name') order by column_name; It will display all columns of your table in alphabetic order.
The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.
Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.
The MySQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in MySQL:
SELECT c.column_name FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.table_name = 'tbl_name' -- AND c.table_schema = 'db_name' ORDER BY c.column_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With