我有2-3个不同的列名,我想在整个数据库中查找,并列出所有具有这些列的表。有没有简单的脚本?


当前回答

对于那些与此相反的搜索,即寻找不包含特定列名的表,这里是查询…

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');

当我们开始缓慢地实现InnoDB特殊的ai_col列的使用,并需要找出200个表中哪些表还没有升级时,这真的很方便。

其他回答

SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';

获取数据库YourDatabase中所有列为columnna或ColumnB的表:

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('columnA','ColumnB')
        AND TABLE_SCHEMA='YourDatabase';

使用这一行查询。将desired_column_name替换为列名。

SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
FROM INFORMATION_SCHEMA.COLUMNS  
WHERE column_name LIKE 'employee%'  
AND TABLE_SCHEMA='YourDatabase'