What's the best way to locate MySQL tables containing specific column names?

Hey everyone! I’m working on a MySQL database and I need some help. I’ve got a few column names (about 2 or 3) that I want to search for across the whole database. I’m trying to figure out which tables have these columns.

Does anyone know if there’s a quick and easy script or method to do this? It would save me a ton of time instead of manually checking each table. I’m not super experienced with MySQL, so any tips or advice would be really helpful.

I’ve tried looking through the MySQL docs, but I’m a bit overwhelmed with all the information. If someone could point me in the right direction or share a simple solution, I’d really appreciate it. Thanks in advance for any help you can offer!

Having dealt with similar situations, I can suggest using a stored procedure for this task. It’s an efficient way to search across multiple tables without writing repetitive queries. Here’s a basic outline:

CREATE PROCEDURE FindColumns(IN columnName VARCHAR(255))
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tableName VARCHAR(255);
DECLARE cur CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE();
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;
read_loop: LOOP
    FETCH cur INTO tableName;
    IF done THEN
        LEAVE read_loop;
    END IF;
    SET @sql = CONCAT('SELECT * FROM ', tableName, ' WHERE 1=0');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    IF FOUND_ROWS() > 0 THEN
        SELECT tableName;
    END IF;
    DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;

END;

You can then call this procedure with CALL FindColumns(‘your_column_name’); This method is particularly useful when dealing with large databases or when you need to perform this search regularly.

hey mate, i use this nifty trick when i need to find columns. try running:

SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE ‘%your_column%’
AND TABLE_SCHEMA = ‘your_db’;

just replace the placeholders. its quick n easy, saves tons of time!

I’ve been in your shoes before, and I found a pretty handy solution. You can query the INFORMATION_SCHEMA database, which stores metadata about your MySQL tables and columns. Here’s a query that worked well for me:

SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (‘column1’, ‘column2’, ‘column3’)
AND TABLE_SCHEMA = ‘your_database_name’;

Replace ‘column1’, ‘column2’, ‘column3’ with your specific column names, and ‘your_database_name’ with the name of your database. This query will return all tables and columns that match your search criteria.

It’s a real time-saver compared to manual checking. Just make sure you have the necessary permissions to access INFORMATION_SCHEMA. Hope this helps!