create table test (id int, ref_typ int, ref int);
create table test_1 (ref1 int, data1 int);
create table test_2 (ref2 int, data2 int);
insert into test (id, ref_typ, ref) values (1, 1, 1);
insert into test (id, ref_typ, ref) values (2, 2, 1);
insert into test (id, ref_typ, ref) values (3, 1, null);
insert into test (id, ref_typ, ref) values (4, 2, null);
insert into test_1 (ref1, data1) values (1, 100);
insert into test_2 (ref2, data2) values (1, 200);
-- führt zum erwarteten Ergebniss in
MySQL, Oracle, PostgreSQL, SQLite, MS-
SQL
select
id, ref_typ, ref, coalesce(data1, data2) as data
from
test
left outer join test_1 on ref1 = ref and ref_typ = 1
left outer join test_2 on ref2 = ref and ref_typ = 2;
-- führt zum erwarteten Ergebniss in
MySQL, Oracle, PostgreSQL, SQLite, MS-
SQL
select
id, ref_typ, ref,
case
when ref_typ = 1 then (select data1 from test_1 where ref1 = ref)
when ref_typ = 2 then (select data2 from test_2 where ref2 = ref)
end as data
from
test;
-- führt zum erwarteten Ergebniss in
MySQL, Oracle, PostgreSQL, SQLite, MS-
SQL, Interbase
select
t.id, t.ref_typ, t.ref,
case
when t.ref_typ = 1 then (select t1.data1 from test_1 t1 where t1.ref1 = t.ref)
when t.ref_typ = 2 then (select t2.data2 from test_2 t2 where t2.ref2 = t.ref)
end as data
from
test t;
drop table test;
drop table test_1;
drop table test_2;