Select from two inherited tables in PostgreSQL -


say there postgresql parent table files empty; it's 'abstract'. , there tables inherit files contain data: red-files, green-files, blue-files.

i can select 3 selecting files*, how can select both red-files , green-files only?

that is, if query retrieve 3 rows red-files , 2 rows green-files, combined query i'm looking show 5 rows.

you can use union unless there field join its.

create table files (     name            text,     size            int );  create table redfiles (     id           char(2) ) inherits (files);   create table bluefiles (     id           char(2) ) inherits (files); 
insert redfiles (name, id, size) values ('file1','aa', 1024); insert redfiles (name, id, size) values ('file2','bb', 2048); insert bluefiles (name, id, size) values ('file3','xx', 1024); insert bluefiles (name, id, size) values ('file4','yy', 1526); 
select * files; 
 name  | size :---- | ---: file1 | 1024 file2 | 2048 file3 | 1024 file4 | 1526 
with myfiles (     select * redfiles     union     select * bluefiles )  select *   myfiles  size = 1024; 
 name  | size | id :---- | ---: | :- file1 | 1024 | aa file3 | 1024 | xx 

dbfiddle here


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -