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.
You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .
USE db_name; DESCRIBE table_name; it'll give you column names with the type.
Get column names from a table using DESC. We can also use the DESC keyword to get the column names.
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.
select group_concat(column_name separator ', ')
from information_schema.columns
where table_name = 'course'
group by table_name
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`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With