Search text in fields in every table of a MySQL database

前端 未结 24 1575
梦谈多话
梦谈多话 2020-11-22 06:23

I want to search in all fields from all tables of a MySQL database a given string, possibly using syntax as:

SELECT * FROM * WHERE * LIKE \'%stuff%\'
         


        
24条回答
  •  旧巷少年郎
    2020-11-22 06:41

    I built on a previous answer and have this, some extra padding just to be able to conveniently join all the output:

    SELECT 
    CONCAT('SELECT ''',A.TABLE_NAME, '-' ,A.COLUMN_NAME,''' FROM ', A.TABLE_SCHEMA, '.', A.TABLE_NAME, 
           ' WHERE ', A.COLUMN_NAME, ' LIKE \'%Value%\' UNION')
    FROM INFORMATION_SCHEMA.COLUMNS A
    WHERE 
            A.TABLE_SCHEMA != 'mysql' 
    AND     A.TABLE_SCHEMA != 'innodb' 
    AND     A.TABLE_SCHEMA != 'performance_schema' 
    AND     A.TABLE_SCHEMA != 'information_schema'
    UNION SELECT 'SELECT '''
    
    -- for exact match use: A.COLUMN_NAME, ' LIKE \'Value\' instead
    

    First you run this, then paste in and run the result (no editing) and it will display all the table names and columns where the value is used.

提交回复
热议问题