Is there a way to search the database if a column name / field name exists in a table in mysql?
4 Answers
If you want to search in the whole database then you should try
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND COLUMN_NAME = 'column_name'
And if you want to search in the particular table then you should try
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name'
Comments
SHOW COLUMNS FROM tablename LIKE 'columnname'
have fun ! :-)
UPDATE:
As mentioned in the comments, this searches only one table, not the whole database (every table). In that case, please refer to DhruvPathak's answer.
2 Comments
DhruvPathak
That would just search 1 table instead of whole database.
Stefan
hmm ok - well i did understand the question if a certain column is in a certain table!