RD Ward
RD Ward

Reputation: 6757

Display ONLY names of colums in MySQL from a table

select FIELD from (show columns FROM users)

This seems like it should work, since show columns is displaying a table of information about the table, however. I don't really understand why this wouldn't display properly, but regardless this may not be the easiest method to extract this information.

Upvotes: 1

Views: 111

Answers (2)

MikeTheReader
MikeTheReader

Reputation: 4190

If you're just looking for the table information you can use:

DESCRIBE users;

If you're looking to actually get the data back in a select, you can use the information_schema.columns table:

select column_name 
  from information_schema.columns 
 where table_name="users" 
   and table_schema = database();

Upvotes: 1

ratsbane
ratsbane

Reputation: 890

You can get the column names, types, and other meta-information from the information schema database. Example:

mysql> select column_name from information_schema.columns where table_schema='test' and table_name='t3';
+-------------+
| column_name |
+-------------+
| col1        |
| col2        |
| col3        |
| col4        |
| col5        |
+-------------+
5 rows in set (0.00 sec)

Upvotes: 5

Related Questions