Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

three table sql query?

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
Avatar of dkim18
dkim18

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial