使用默认sql表查找表中存在的列

时间:2015-03-02 11:04:28

标签: postgresql

我设置了一个postgresql数据库,其中客户数据按模式分割。

在sql中我想为表产品确定哪些列包含字符串" id"存在

我尝试使用pg _...表来识别这些列,但是下面的查询似乎是从跨模式中恢复结果,尽管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子句不正确 - 任何帮助都会受到赞赏

1 个答案:

答案 0 :(得分: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';