Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get table column names in MySQL?

Tags:

sql

php

mysql

Is there a way to grab the columns name of a table in MySQL using PHP?

like image 204
An employee Avatar asked Oct 06 '09 16:10

An employee


People also ask

How do I get the column names of a table?

To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'

How can you list all columns for a given table MySQL?

To list all columns in a table, we can use the SHOW command. Let us first create a table. Syntax to list all column names.

How do I find columns in MySQL?

SELECT table_name, column_name from information_schema. columns WHERE column_name LIKE '%column_name_to_search%'; Remember, don't use % before column_name_to_search if you know the starting characters of that column.

How can I get only column names from a table in SQL?

In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS .


1 Answers

You can use DESCRIBE:

DESCRIBE my_table; 

Or in newer versions you can use INFORMATION_SCHEMA:

SELECT COLUMN_NAME   FROM INFORMATION_SCHEMA.COLUMNS   WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table'; 

Or you can use SHOW COLUMNS:

SHOW COLUMNS FROM my_table; 

Or to get column names with comma in a line:

SELECT group_concat(COLUMN_NAME)   FROM INFORMATION_SCHEMA.COLUMNS   WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table'; 
like image 54
Greg Avatar answered Sep 17 '22 01:09

Greg