Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, i want to get image path from table2 by using left join operation in asp.net .but didn't get it..so please help me..

here my query is:
SQL
SELECT n.newsid,n.state,n.district,n.region,n.views,n.currentplace,n.newstitle,n.description,n.poston,n.postby,ni.imagepath,ni.newsid 
FROM  tbl_userpost_news n 
LEFT JOIN tbl_userpost_newsimages ni ON n.newsid = ni.newsid 
AND (SELECT TOP 1 * FROM ni WHERE n.newsid=ni.newsid)  
Order By n.poston desc


please send current code for that.......thank you.
Posted
Updated 28-Aug-15 23:13pm
v2

Your AND condition is being applied to the JOIN...ON, so it doesn't match properly.
Move the TOP 1 bit:
SQL
SELECT TOP 1  n.newsid,n.state,n.district,n.region,n.views,n.currentplace,n.newstitle,n.description,n.poston,n.postby,ni.imagepath,ni.newsid 
FROM  tbl_userpost_news n 
LEFT JOIN tbl_userpost_newsimages ni ON n.newsid = ni.newsid 
ORDER BY n.poston desc
And it shoudl work.
 
Share this answer
 
Comments
Member 10575434 30-Aug-15 3:45am    
it is right, but i want top1 from images table..any way thank you for supporting me..
If you want only the image column from the second table then you can use subquery to that.
Check this query-
SQL
SELECT n.newsid, n.state, n.district, n.region, n.views, n.currentplace, n.newstitle, n.description, n.poston, n.postby, (SELECT TOP 1 imagepath FROM tbl_userpost_newsimages ni WHERE n.newsid=ni.newsid) AS imagepath
FROM  tbl_userpost_news n
Order By n.poston desc


Note: I have removed ni.newsid from select list as n.newsid is already there and suppose to return the same value.

Hope, it helps :)
 
Share this answer
 
Comments
Member 10575434 30-Aug-15 3:43am    
THAnk you this is my expected query ....thanks
Suvendu Shekhar Giri 30-Aug-15 12:52pm    
Glad that it helped :)
 
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