Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a subset from another table

Status
Not open for further replies.

gussifinknottle

Programmer
Jan 16, 2001
19
US
Hello,

I would like to retrieve all of the data from table1 wherein two of the columns match two columns in table2.

I have done a select * from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)

I am not getting the right number though? What do I need to modify?

Thanks,
Gussi.

 
YOu would be better joining tables

select t1.* from table1 t1, table2 t2
where
t1.col1= t2.col1

Ian
 
The only problem with the join is that table2 may not be uniquely keyed on col1 and col2, so it's possible this could increase the number of rows your query returns. If that's the case, an alternative would be to use an EXISTS:

Code:
select * from table1 
where 
exists
(select 1 
from table2 t2
where t2.col1 = t1.col1
and   t2.col2 = t1.col2)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top