Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Search across table

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2002-05-26 : 23:31:33

I am creating a SQL statement as such:

SELECT userID FROM TBLUSERDETAILS

How can I only bring back the records if the userID in TBLUSERDETAILS is also found in TBLPHOTO.??

Im not sure where to go with this, I'm thinking something along the lines of a join?

Thanks alot,
mike




jbkayne
Posting Yak Master

100 Posts

Posted - 2002-05-26 : 23:37:18
SELECT TBLUSERDETAILS.userID
FROM TBLUSERDETAILS
inner join TBLPHOTO
on TBLUSERDETAILS.userId = TBLPHOTO.userId

or

SELECT userID
FROM TBLUSERDETAILS
WHERE EXISTS (
select * from TBLPHOTO where userId = TBLUSERDETAILS.userID
)




Edited by - jbkayne on 05/26/2002 23:39:29
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2002-05-26 : 23:48:41


Is one of those particularly faster than the other?

thanks alot,
mike

Go to Top of Page

dataphile
Yak Posting Veteran

71 Posts

Posted - 2002-05-28 : 07:51:58
The first one will be faster.

The second one could give you inconsistent results if there are no referential constraints defined between the two tables.


Go to Top of Page
   

- Advertisement -