return all the columns in a MySQL table in a strin

2019-06-24 17:11发布

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.

标签: mysql sql mysql5
3条回答
可以哭但决不认输i
2楼-- · 2019-06-24 17:22
select group_concat(column_name separator ', ') 
from information_schema.columns 
where table_name = 'course' 
group by table_name
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-24 17:27

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

查看更多
够拽才男人
4楼-- · 2019-06-24 17:49

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`
查看更多
登录 后发表回答