How to find all the tables in MySQL with specific column names in them?

后端 未结 11 1051
天命终不由人
天命终不由人 2020-11-22 11:09

I have 2-3 different column names that I want to look up in the entire DB and list out all tables which have those columns. Any easy script?

11条回答
  •  情歌与酒
    2020-11-22 11:32

    For those searching for the inverse of this, i.e. looking for tables that do not contain a certain column name, here is the query...

    SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE 
    TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT 
    TABLE_NAME FROM information_schema.columns WHERE column_name = 
    'column_name' AND TABLE_SCHEMA = 'your_db_name');
    

    This came in really handy when we began to slowly implement use of InnoDB's special ai_col column and needed to figure out which of our 200 tables had yet to be upgraded.

提交回复
热议问题