PDA

View Full Version : List all tables containing a given column name


Sabine
2015-12-22, 04:53 AM
How do a I list all tables containing a given column name?

I found there are several tables have the same field name, called: product_id.

Any method to find all tables that contain product_id?

Third Lake
2015-12-22, 04:56 AM
You can use the INFORMATION_SCHEMA database and the COLUMNS table in particular Example of use:

SELECT
table_name,
column_name,
data_type,
ordinal_position

FROM INFORMATION_SCHEMA.COLUMNS

WHERE table_schema = 'myDatabase' --- the database you want to search
AND column_name = 'name' ; --- or: column_name LIKE '%name%'


say, you want to check a database called cheap_eyeglasses, here is your MySQL command:

SELECT
table_name,
column_name,
data_type,
ordinal_position

FROM INFORMATION_SCHEMA.COLUMNS

WHERE table_schema = 'cheap_eyeglasses' --- the database you want to search
AND column_name = 'product_id' ; --- or: column_name LIKE '%name%'