return all the columns in a MySQL table in a strin

2019-06-24 17:25发布

问题:

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.

回答1:

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


回答2:

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`


回答3:

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



标签: mysql sql mysql5