Помогите написать SQL запрос! Есть 3 таблицы с уникальными полями и общим полем индекса Table 1: Table 2: Table 3: id p1 p2 id p3 p4 id p5 p6 1 1 1 1 1 1 2 1 0 2 1 0 4 0 1 4 0 0 3 1 0 5 0 1 6 0 0 4 0 1 6 1 0 7 1 1 (Это пример трех таблиц) Надо получить следующую таблицу id p1 p2 p3 p4 p5 p6 1 1 1 1 1 - - 2 1 0 - - 1 0 3 1 0 - - - - 4 0 1 0 1 0 0 5 - - 0 1 - - 6 - - 1 0 0 0 7 - - - - 1 1 Символ - означает, что в других таблицах нет подобных полей, а значит и данных. Можно оставить поля пустыми, а можно и забить 0 в результате выполнения запроса. По всем вопросам [email protected]
select t1.id as id,t1.p1,t1.p2,t2.p3,t2.p4,t3.p5,t3.p6 from table1 t1 left join table2 t2 on (t1.id=t2.id) left join table3 t3 on (t1.id=t3.id) union all select t2.id as id,t1.p1,t1.p2,t2.p3,t2.p4,t3.p5,t3.p6 from table1 t1 right join table2 t2 on (t1.id=t2.id) left join table3 t3 on (t2.id=t3.id) where t1.id is null union all select t3.id as id,t1.p1,t1.p2,t2.p3,t2.p4,t3.p5,t3.p6 from table1 t1 right join table3 t3 on (t3.id=t1.id) left join table2 t2 on (t2.id=t3.id) where t1.id is null and t2.id is null order by 1 pegas добавил [date]1092599613[/date]: или вот так, но такой синтаксис IB 5.6 скорее всего не поймет select iif(t1.id is not null,t1,iif(t2 is not null,t2,t3)),t1.p1,t1.p2,t2.p3,t2.p4,t3.p5,t3.p6 from table1 t1 full outer join table2 t2 on (t1.id=t2.id) full outer join table3 t3 on (t2.id=t3.id or t1.id=t3.id) order by 1