I have a parent table called Animal(Name, Age, Height, Id) and the PK is Id
And three child tables that extend Animal
providing extra columns:
Dog(Breed, ...) INHERITS(Animal)
Cat(Color, ...) INHERITS(Animal)
Bird(BeakType, ...) INHERITS(Animal)
Given an id
eg 2 I am trying to find the row in any of the three sub-tables where the id exists. While I can do select * from animal
it does not give me the extra columns for id
I am looking for.
I have tried to do a natural join on all four tables but it is giving me nothing.
select * from animal natural join dog natural join cat natural join bird
please help me in what i am doing wrong. thank you very much.
EDIT: or is there an easier way to get the extra columns defined for eg dog if id=2 belongs to dog??
For any animal to survive your existing query it would need to be a dog and also to be a cat and also to be a bird. Each "natural join" will limit to rows returned by an exact match - and here you cannot match on all three.
You need to use an "outer" join, these allow unmatched rows to also be returned, try something like this:
select a.*, d.*, c.*, b.*
from animal a
left outer join dog d on a.id = d.id
left outer join cat c on a.id = c.id
left outer join bird b on a.id = b.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.