2

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Maxim Gotovchits
  • 729
  • 3
  • 11
  • 22

2 Answers2

4

use table information_schema.columns

select column_name 
from information_schema.columns 
where table_name = 'my_table_name' and ordinal_position = 2;
Drew
  • 24,851
  • 10
  • 43
  • 78
Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
0

You can select from INFORMATION_SCHEMA.COLUMNS and use limit

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table'
limit 1, 1;
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175