Question

Is there a way to return all the columns in a MySQL table in a string format?

I would like to have something like this returned:

course_id, name, par, yds, mtrs etc.

I am aware of how to show the fields/columns in a table (SHOW FIELDS FROM course;) however these are returned in a tabular format.

Was it helpful?

Solution

select group_concat(column_name separator ', ') 
from information_schema.columns 
where table_name = 'course' 
group by table_name

OTHER TIPS

If your DB-user has access to the information schema, you can use the following:

SELECT GROUP_CONCAT( `COLUMN_NAME` )
FROM `information_schema`.`COLUMNS`
WHERE `TABLE_SCHEMA` = 'your_database_name'
AND `TABLE_NAME` = 'your_table_name'
GROUP BY `TABLE_NAME`

SELECT GROUP_CONCAT(COLUMN_NAME,',') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tablename'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top