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

后端 未结 11 936
天命终不由人
天命终不由人 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:15

    If you want "To get all tables only", Then use this query:

    SELECT TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME like '%'
    and TABLE_SCHEMA = 'tresbu_lk'
    

    If you want "To get all tables with Columns", Then use this query:

    SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
    FROM INFORMATION_SCHEMA.COLUMNS  
    WHERE column_name LIKE '%'  
    AND TABLE_SCHEMA='tresbu_lk'
    
    0 讨论(0)
  • 2020-11-22 11:17
    SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
    FROM INFORMATION_SCHEMA.COLUMNS  
    WHERE column_name LIKE 'employee%'  
    AND TABLE_SCHEMA='YourDatabase'
    
    0 讨论(0)
  • 2020-11-22 11:21
    SELECT TABLE_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME LIKE '%wild%';
    
    0 讨论(0)
  • 2020-11-22 11:22
    SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
    
    0 讨论(0)
  • 2020-11-22 11:30

    More simply done in one line of SQL:

    SELECT * FROM information_schema.columns WHERE column_name = 'column_name';
    
    0 讨论(0)
  • 2020-11-22 11:30

    Use this one line query, replace desired_column_name by your column name.

    SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
    
    0 讨论(0)
提交回复
热议问题