检索包含列组合的所有表名

时间:2018-07-06 08:49:51

标签: sql postgresql

是否有一种方法可以从包含两个或多个列(column1和column2)组合的数据库中选择所有表名? 我认为这不是正确的方法:

f

1 个答案:

答案 0 :(得分:2)

尝试使用包含CTEarray aggregator。运算符<@将查找元素的确切组合。

WITH j AS (
  SELECT table_name, array_agg(column_name::TEXT) AS columns
  FROM information_schema.columns 
  GROUP BY table_name)
SELECT * FROM j
WHERE '{column1,column1}' <@ j.columns

示例:

WITH j AS (
  SELECT table_name, array_agg(column_name::TEXT) AS columns
  FROM information_schema.columns 
  GROUP BY table_name)
SELECT * FROM j
WHERE '{name,statement}' <@ j.columns;

       table_name       |                              columns                               
------------------------+--------------------------------------------------------------------
 pg_prepared_statements | {name,statement,prepare_time,parameter_types,from_sql}
 pg_cursors             | {name,statement,is_holdable,is_binary,is_scrollable,creation_time}
(2 Zeilen)