Monday, July 31, 2023

How to get the table name with a required column name in MySQL using query

While working on a project we had a big database which contains more than 250 tables. I wanted to get the table name containing a column say "Column A". We can do it easily with a query.

Say now we have a database called as "Test_Database" with lot of tables and columns. Now I want to get all the table names in this database which contains "Column A" or "Column B". We can execute the below query to get the table names:

SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('Column A','Column B'
AND TABLE_SCHEMA='Test_Database'; 


Above query shows all the table names which contains "Column A" or "Column B" columns. That's it. Happy coding !

No comments: