EN
MySQL - find all tables with specific column names
0
points
In this article, we would like to show you how to find all tables with specific column names in MySQL
Quick solution:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('column1','column2', 'columnN')
AND TABLE_SCHEMA='your_database';
Practical example
Let's say we have a database named dirask that contains users table and admins table, both with the following columns:
idnamesurnameemail
and locations table with columns:
idaddress
Example 1
In this example, we will display all the tables from dirask database that contain name OR surname columns.
Query:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('name','surname')
AND TABLE_SCHEMA='dirask';
Output:
Example 2
In this example, we will display all the tables from dirask database that contain id column.
Query:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('id')
AND TABLE_SCHEMA='dirask';
Output: