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 2005 Forums
 Transact-SQL (2005)
 query question

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2008-11-24 : 14:41:18
I have 2 tables Cars and CarPics

Cars-
CarId
CarTypeId
Carname

CarPics-
CarPicId
CarId
Image
ImageDefault - This is a bit

I want to do a select That brings back a certain CarTypeId. I want to join it to the CarPics table and bring back just the one pic that is makers in the ImageDefault field. There is only one image marked as default for each CarPicId


Cars

1 2 Mustang
2 2 Corvette


CarPics

1 1 Pic1 1
2 1 Pic2 0
3 2 pica 0
4 2 picb 1


So for a car 2 CarTypeID. This is what I want to bring back

1 Mustang Pic1
4 Corvette picb

Dave
Helixpoint Web Development
http://www.helixpoint.com

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-11-24 : 17:41:01
select cp.carpicid,cr.carname,cp.image
from Cars cr inner join Carpics cp
on cr.carid = cp.carid
and cp.ImageDefault = 1
Go to Top of Page
   

- Advertisement -