fikir
Programmer
- Jun 25, 2007
- 86
hello all, I need your help on this
I have two tables
tbl1
col1 col2
1 aa
2 bb
3 cc
and tbl2
col1 col2
1 dd
2 ee
I want to write a simple query so that,
I select records from tbl1 on two conditions
i. if records exist in tbl2 , then their matching records will be selected from tbl1
ii. if no record exists in tbl2, all records from tbl1 will be selected from tbl1 regardless of any thing
I wrote this like
select *
from tbl1
where col1 in (select col1 from tbl2)
or not exists (select col1 from tbl2)
but this is taking a lot of time,
is there an easy and efficient way to write it
Thanks
I have two tables
tbl1
col1 col2
1 aa
2 bb
3 cc
and tbl2
col1 col2
1 dd
2 ee
I want to write a simple query so that,
I select records from tbl1 on two conditions
i. if records exist in tbl2 , then their matching records will be selected from tbl1
ii. if no record exists in tbl2, all records from tbl1 will be selected from tbl1 regardless of any thing
I wrote this like
select *
from tbl1
where col1 in (select col1 from tbl2)
or not exists (select col1 from tbl2)
but this is taking a lot of time,
is there an easy and efficient way to write it
Thanks