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,quan lity)
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
I have three tables
tblOne(oneid,twoid,threeid
tblTwo(twoid,amount)
tblThree(threeid,type,quan
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
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
select * from tblOne a
Where
a.twoid in (select twoid from tblTwo) OR
a.threeid in (Select threeid from tblThree)