Using a SHOW statement in a subquery

I want to write an SQL statement that returns ONLY the column names for a given table. "SHOW COLUMNS FROM `table_name`;" Shows the data I want (and more). In order to get the data I need, every column from this statement must be removed except the `Field` column.

I have tried using a subquery with SELECT, but keep getting a syntax error. "SELECT `Field` FROM (SHOW COLUMNS FROM `table_name`)" Any ideas how to accomplish this?

Answer Answered by Ian Gilfillan in this comment.

You could use the Information Schema Columns table, for example:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
  WHERE TABLE_SCHEMA='test' AND TABLE_NAME='y';
+-------------+
| COLUMN_NAME |
+-------------+
| a           |
| b           |
+-------------+

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.