Retrieve all tables having specified columns in MySQL

0
2554
MySQL Database
MySQL Database

Just a simple trick to retrieve all tables in database that have columns with specified names.


SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME = 'COLUMN_NAME'
        AND TABLE_SCHEMA = 'DATABASE_NAME';

You can group to search for more columns by providing a list of column names.


SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('COLUMN_NAME_1', 'COLUMN_NAME_2')
        AND TABLE_SCHEMA = 'DATABASE_NAME';

These are just examples to show that all columns’ information are stored in INFORMATION_SCHEMA.COLUMNS, you can query and filter to suit your requirements.