return all the columns in a MySQL table in a string format

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:06:15

问题


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'



来源:https://stackoverflow.com/questions/11341192/return-all-the-columns-in-a-mysql-table-in-a-string-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!