PostgreSQL查询列名所在的表
使用信息模式中的pg_tables和pg_attribute视图来构建查询。以下是一个查询示例,它会返回所有包含名为your_column_name的列的表名:
SELECT t.table_name
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
JOIN information_schema.tables t ON n.nspname = t.table_schema
WHERE a.attname = 'your_column_name'
AND a.attnum > 0
AND NOT a.attisdropped
AND t.table_schema NOT IN ('pg_catalog', 'information_schema')
AND c.relname = t.table_name
GROUP BY t.table_name;
请将your_column_name替换为你想查找的列名。