Applying joins conditionally in SQL Server -
i have set of records, have select records set have theeir id in either of 2 tables.
suppose have table1 contains
id name ---------- 1 name1 2 name2
now need select records table 1 have either id in table2 or in table3
i trying apply or operator witin inner join like:
select * table1 inner join table2 on table2.id = table1.id or inner join table3 on table3.id = table1.id.
is possible? best method approach this? not able use
if exist(select 1 table2 id=table1.id) select table1
could me on this?
i inclined use exists
:
select t1.* table1 t1 exists (select 1 table2 t2 t2.id = t1.id) or exists (select 1 table3 t3 t3.id = t1.id) ;
the advantage using exists
(or in
) on join involves duplicate rows. if table2
or table3
have multiple rows given id
, version using join
produce multiple rows in result set.
Comments
Post a Comment