Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL
select JobAds.AdID, JobAds.Title, JobAds.Description, JobAds.Date, AdPhotos.PhotoName,
State.StateName, City.CityName, SubCategory.Name, JobAds.Date
from State,City,SellerDetails,JobAds, AdPhotos ,SubCategory
where State.StateID = SellerDetails.StateID
and City.CityID = SellerDetails.CityID
and JobAds.SellerID = SellerDetails.SellerID
and JobAds.AdID = AdPhotos.AdID
and JobAds.SubCategoryID = SubCategory.SubCategoryID
order by JobAds.Date desc


in above query,in AdPhotos table, i have columns--
AdID PhotoName
1 photo1.jpg
1 photo2.jpg
1 photo3.jpg
2 photo4.jpg
As you can see for AdID 1 i have 3 photos but i want only the first photo for an AdID using above in above query.

Please help me out quckly,otherwise i'll lose my job
Posted
Comments
[no name] 7-Jun-14 7:59am    
"help me out quckly,otherwise i'll lose my job", you would think that after 3 years, you would have figured out how the site works.

You can use group by id and then take the first item in each group. Check below answers
so q1
[^]
so q2[^]
 
Share this answer
 
Add this before the order by clause:
SQL
and AdPhotos.PhotoName in
(select top 1 PhotoName from AdPhotos where AdID = JobAds.AdID)

Hope that helps keep your job. Good luck.
 
Share this answer
 
u can use sub query

SQL
(select top 1 PhotoName from AdPhoto Where AddId=JobAds.AdID) as PhotoName


you can place this query place of AdPhotos.PhotoName

gud luck
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900