Languages
[Edit]
EN

MySQL - find all tables with specific column names

0 points
Created by:
Khloe-Kaiser
578

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:

  • id
  • name
  • surname
  • email

andĀ locationsĀ table with columns:

  • id
  • address

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:

MySQL - find all tables with specific column names - result
MySQL - find all tables with specific column names - result

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:

MySQL - find all tables with specific column name - result
MySQL - find all tables with specific column name - result
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join