Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mysql

mysql5

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.

like image 675
crmpicco Avatar asked Jul 05 '12 09:07

crmpicco


People also ask

How show all columns in MySQL table?

You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .

How do I get a list of all columns of a table in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.

How can I get only columns from a table in MySQL?

Get column names from a table using DESC. We can also use the DESC keyword to get the column names.

How do I return a column in MySQL?

Retrieving All Columns If you want to retrieve the data from every column in a table, you do not need to specify each column name after the SELECT keyword. Use the asterisk character (*) in place of a column list in a SELECT statement to instruct MySQL to return every column from the specified table.


2 Answers

select group_concat(column_name separator ', ') 
from information_schema.columns 
where table_name = 'course' 
group by table_name
like image 96
juergen d Avatar answered Sep 20 '22 06:09

juergen d


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`
like image 39
feeela Avatar answered Sep 20 '22 06:09

feeela