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!

nested select

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Can anyone show me how i can do this

SELECT * , (
SELECT pic_path
FROM tbl_newspics
WHERE tbl_newspics.pic_newsid = tbl_news.news_id
LIMIT 1 ) AS pic
FROM tbl_news
WHERE news_publish =1
ORDER BY news_added DESC
LIMIT 5

Basically I have a news table and a pictures table. i want to be able to link them but only have the first picture joined with the news

}...the bane of my life!
 
Code:
SELECT N.news_id
     , N.news_added
     , N.other_columns
     , P.pic_path
  FROM tbl_news as N
INNER
  JOIN tbl_newspics as P
    ON P.pic_newsid = N.news_id  
   AND P.somecolumn =
       ( select MIN(somecolumn)
           from tbl_newspics
          where pic_newsid = N.news_id )
 WHERE N.news_publish =1
ORDER  
    BY N.news_added DESC 
LIMIT 5
use LEFT OUTER JOIN instead of INNER JOIN, if it is possible to have a news without a pic

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top