Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql get table column names in alphabetical order

Tags:

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.

like image 524
John Scipione Avatar asked Sep 24 '10 21:09

John Scipione


People also ask

How do I get column names in alphabetical order?

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.

How do I get column names in alphabetical order in SQL?

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.

How do I sort a column alphabetically in MySQL?

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.

How do I get alphabetical order in MySQL?

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.


1 Answers

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 
like image 123
OMG Ponies Avatar answered Sep 21 '22 13:09

OMG Ponies