three table sql query?

dkim18
dkim18 used Ask the Experts™
on
I need a little help with sql work.

I have three tables
tblOne(oneid,twoid,threeid,xxx)
tblTwo(twoid,amount)
tblThree(threeid,type,quanlity)

I want to find records from tblOne with:
1) any twoid from tblTwo table
2) anythreeid from tblthree table

basically, I want to find records from tblOne when #1 or #2 happends.

Do I need to create two sql finding records from each table and combine?
Can I do this in one sql?

When I tried to inner join, it didn't bring me back everything.

select * from tblOne a inner join tblTwo b on a.twoid = b.twoid
inner join tblThree c on a.threeid = c.threeid
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I think I got it.

select * from tblOne a
Where
a.twoid in (select twoid from tblTwo) OR
a.threeid in (Select threeid from tblThree)
you could also do, it could perform better.

select * from tblOne a
Where exists(select 1 from tbltwo where twoid = a.twoid) and
exists(select 1 from tbltwo where threeid = a.threeid)
Information Technology Specialist
Commented:
I think ralmada's query should be a "where exists ... OR exists", not "... and exists".
You could also use -
select a.* from tblOne a, tblTwo b, tblThree c
where a.twoid = b.twoid or a.threeid = c.threeid;

Although I suspect using exists may very well perform better.
yeah, wrong logical operator

OR instead of AND

select * from tblOne a
Where exists(select 1 from tbltwo where twoid = a.twoid) OR
exists(select 1 from tbltwo where threeid = a.threeid)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial