2015-03-02 16 views
0

我有一个postgresql数据库设置与模式分割的客户端数据。使用默认的sql表来查找表中存在的列

在SQL中我想找出其中存在

我试图使用PG _...表,以确定这些列包含字符串“ID”列的表的产品,但出现下面的查询要带回从整个模式的结果,尽管限制在TABLE_SCHEMA

SELECT * 
FROM pg_class c 
    INNER JOIN pg_attribute a ON a.attrelid = c.oid 
    INNER JOIN pg_type t ON a.atttypid = t.oid 
    INNER JOIN information_schema.tables sch ON c.relname = sch.table_name 
WHERE c.relname = 'products' 
AND a.attnum > 0 
AND a.attname LIKE '%id%' 
AND table_schema = 'schema001' 

我猜的模式可能设置不正确或where子句是不正确 - 任何帮助,将不胜感激

回答

0

使用pg_namespace和降information_schema:

SELECT nspname, relname, attname 
FROM pg_class c 
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace 
    INNER JOIN pg_attribute a ON a.attrelid = c.oid 
    INNER JOIN pg_type t ON a.atttypid = t.oid 
WHERE c.relname = 'products' 
AND a.attnum > 0 
AND a.attname LIKE '%id%' 
AND n.nspname = 'schema001'; 
+0

谢谢你的工作很棒!你知道我在哪里可以找到关于这些表格的一些文件吗?由于它们非常方便但难以解释 – 2015-03-02 11:36:25

+0

它们是系统目录的一部分:http://www.postgresql.org/docs/current/interactive/catalogs.html – 2015-03-02 11:38:32

+0

再次感谢您的帮助 – 2015-03-02 11:48:38

相关问题