Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hello everyone,I am stuck with a query, i cannot make it work.I have 2 tables:NEWS ID | TITLE | TEXT ------------------- 1 | title1 | text1 2 | title2 | text2 3 | title3 | text3 IMAGESID | IDNEW | PATH------------------1 | 1 | path12 | 1 | path23 | 1 | path34 | 2 | path45 | 2 | path56 | 3 | path6 I need to get all the news, and for each new i need the id of only 1 of the possible images.I thought it could be an inner join but it does not work:SELECT NEWS.*, IMAGES.ID AS IDIMAGE FROM NEWSINNER JOIN IMAGESON NEWS.ID = IMAGES.IDNEWCan someone please help me with this query?Thanks in advance.
mwjdavidson
Aged Yak Warrior
735 Posts
Posted - 2007-09-20 : 04:20:53
If you don't care which Image ID you return:
SELECT NEWS.ID, NEWS.TITLE, NEWS.TEXT, MIN(IMAGES.ID) AS IDIMAGE FROM NEWSINNER JOIN IMAGESON NEWS.ID = IMAGES.IDNEWGROUP BY NEWS.ID, NEWS.TITLE, NEWS.TEXT
Mark
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts
Posted - 2007-09-20 : 04:20:55
[code]SELECT NEWS.*, IMAGES.ID AS IDIMAGE FROM NEWSINNER JOIN(select * from Images t2where t2.Path = (select max(Path) from Images t3 where t3.IDNEW = t2.IDNEW)) as IMAGESON NEWS.ID = IMAGES.IDNEW[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"