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

select table_name from information_schema.columns where column_name = 'column1';

最佳答案

尝试使用包含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)

关于sql - 检索包含列组合的所有表名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51206448/

10-15 19:47