2017-04-07 1 views
0

PostgreSQLの親テーブルfilesが空であるとします。それは「抽象的」です。そして、データを含むfilesから継承する表がある:red-filesgreen-filesblue-filesPostgreSQLの2つの継承テーブルから選択

files*から3種類の中から選択できますが、どのようにred-filesgreen-filesから選択すればよいですか?

つまり、クエリがred-filesから3行、green-filesから2行を取得する場合、私が探している組み合わせクエリでは5行が表示されます。

答えて

1

結合するフィールドがない限り、UNION ALLを使用できます。

CREATE TABLE files (
    name   text, 
    size   int 
); 

CREATE TABLE redfiles (
    id   char(2) 
) INHERITS (files); 


CREATE TABLE bluefiles (
    id   char(2) 
) INHERITS (files); 
insert into redfiles (name, id, size) values ('file1','aa', 1024); 
insert into redfiles (name, id, size) values ('file2','bb', 2048); 
insert into bluefiles (name, id, size) values ('file3','xx', 1024); 
insert into bluefiles (name, id, size) values ('file4','yy', 1526); 
select * from files; 
 
name | size 
:---- | ---: 
file1 | 1024 
file2 | 2048 
file3 | 1024 
file4 | 1526 
with MyFiles as 
(
    select * from redfiles 
    union all 
    select * from bluefiles 
) 
select * 
from MyFiles 
where size = 1024; 
 
name | size | id 
:---- | ---: | :- 
file1 | 1024 | aa 
file3 | 1024 | xx 

dbfiddle here

+0

元の質問に編集を参照してください - うまくいけば、今の私の質問は明確です。 – jensph

+0

各テーブルのクエリを別々に作成し、次にユニオンを適用します。したがって、単一のクエリをテーブルに一緒に適用する方法はありません。 'files'には' size'という列名があります。 'where size> 500'をすべてのテーブルに適用するには、テーブルの個々のクエリごとに記述する必要があります。 – jensph

+0

両方のテーブルを結合できるフィールドはありますか? – McNets

関連する問題