簡體   English   中英

PostgreSQL - 基於列數的表聯合

[英]PostgreSQL - UNION of tables based on number of columns

我在 PostgreSQL 中工作,我想聯合一個名為“postgres”的數據庫的 11 列的所有表。

提到的表的列表,我通過以下查詢得到它。

with  
completo_tablas as (
select column_name, table_name from information_schema.columns
where table_schema='public'
)
--,tablas as (
select table_name--, count(column_name)
from completo_tablas
group by table_name
having count(column_name)=11
--)

Obteining,類似於:

Table_name
--------------  
table1  
table2  
.  
.  
.  

我無法解決的問題是,無法獲得特定日期的 11 列表格的 UNION; 我知道我可以做的不僅僅是復制表名,例如:

Select * from tabla1 UNION select * from tabla2 UNION Sel....

鑒於我們業務的性質,桌子的數量會隨着時間的推移而增加。

由於我們業務的性質,我想要的是“動態”查詢,其中我沒有在 UNION 查詢中手動設置的表的名稱。

但是你總能得到的東西,所有 11 列表的聯合

感謝您的時間。

您可以嘗試參考下表來簡化您的任務,但您應該同意這確實是一個奇怪的要求,因為您無法將數據保留為您的數據庫對象,同樣沒有其他標准方法可以解決此問題。

 \o path/file.sql

  with  
completo_tablas as (
select column_name, table_name from information_schema.columns
where table_schema='public'
)
,tablas as (
select table_name-- , count(column_name)
from completo_tablas
group by table_name
having count(column_name)=11
)
 select 'select * from '|| table_name||' union'
 from tablas

 \o

  \i file.sql

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM