select - SQL selecting not surely existing column from on table or another -
i use generalisation/specialize table zadavatel (which contains primary key) either table sukromna_osoba or firma (both contains foreign key points on zadavatel). need select sukromna_osoba table if sukromna_osoba.meno = 'string' exists or firma table if firma.nazov_firmy = 'string' exists, both if both conditions true. need in 1 select.
create table zadavatel ( id_zadavatela integer, adresa varchar(25) ); create table sukromna_osoba ( id_sukromnej_osoby integer, meno varchar(20), mobil integer, email varchar(20) ); create table firma ( id_firmy integer, nazov_firmy varchar(20), ico integer, bankove_spojenie integer );
id_zadavatela primary key, , id_sukromnej_osoby , id_firmy foreign keys points @ id_zadavatela.
i tried this:
select pr.id_projektu, pr.popis, zad.id_zadavatela, fi.nazov_firmy projekt pr join zamestnanec zam on pr.manazer = zam.osobne_cislo join zadavatel zad on pr.zadavatel = zad.id_zadavatela join firma fi on zad.id_zadavatela = fi.id_firmy zam.meno = 'jan novák' or ( select pr1.id_projektu, pr1.popis, zad1.id_zadavatela, so1.meno projekt pr1 join zamestnanec zam1 on pr1.manazer = zam1.osobne_cislo join zadavatel zad1 on pr1.zadavatel = zad1.id_zadavatela join sukromna_osoba so1 on zad1.id_zadavatela = so1.id_sukromnej_osoby zam1.meno = 'jan novák' )
since not know if it's firm or person, use left outer join on both, this:
select pr.id_projektu, pr.popis, zad.id_zadavatela, fi.nazov_firmy, so.meno projekt pr join zamestnanec zam on pr.manazer = zam.osobne_cislo join zadavatel zad on pr.zadavatel = zad.id_zadavatela left outer join firma fi on zad.id_zadavatela = fi.id_firmy left outer join sukromna_osoba on zad.id_zadavatela = so.id_sukromnej_osoby zam.meno = 'jan novák'
one of 2 result columns, nazov_firmy
or meno
, null
.
Comments
Post a Comment