Maxim Gotovchits Maxim Gotovchits - 1 year ago 144
MySQL Question

MySQL. How to get column name by index?

How can I get a column name by its index using sql query in command line?

I expect something like

show columns from my_table where col_index = 2;

I found many answers about how to show all columns but there's no answer on how to get exact column name by its position in the table.

Answer Source

use table information_schema.columns

select column_name 
from information_schema.columns 
where table_name = 'my_table_name' and ordinal_position = 2;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download