2

I know you can use DESCRIBE tablename; to get the description of the table, which includes Field, Type, Null, Key, Default, and Extra. However, is there a way to get just the Field column and not the rest (preferably in a format that is easy to work with, like in Python for example)? I know I could do some parsing in Python, but I would prefer to get it from MySQL in the cleanest form.

I found a similar question here, but none of them answer my question.

For what it's worth, I'm interfacing with MySQL via PyMySQL in Python3 and I'm using MySQL server version 5.1.73. Thanks for your help!

Community
  • 1
  • 1
Logan
  • 1,575
  • 1
  • 17
  • 26

1 Answers1

4

As per my comment above, and your own link, and the first thing that comes to mind, how about

select column_name from INFORMATION_SCHEMA.COLUMNS
where table_schema='stackoverflow'
and table_name='questions'

my db name and table name are in the where clause

Drew
  • 24,851
  • 10
  • 43
  • 78
  • Thanks for the help Drew! I ended up getting what I wanted with `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='...';` And as for the formatting, I guess I'll just stick with the list of dictionaries it gives in Python. – Logan Apr 24 '16 at 03:57
  • @Logan you **need** `AND TABLE_SCHEMA='...'` in your query, because otherwise as soon as a table with the same name is created in a different schema on the same server, your query will give you incorrect results. If the order of the column names is important, you should also add `ORDER BY ORDINAL_POSITION` so that the order of the results is deterministic. – Michael - sqlbot Apr 24 '16 at 17:15