Tuesday, November 20, 2012

Oracle select with subquery

Try this:
SELECT "NEWS"."NEWSID" as ID,
   "NEWS"."SLUG",
   "NEWS_TRANSLATION".*, 
   (SELECT * FROM (SELECT FILENAME FROM NEWS_MEDIA WHERE NEWSID = ID ORDER BY POSITION ASC) AND rownum = 1) as FILENAME
FROM "NEWS" 
INNER JOIN "NEWS_TRANSLATION" ON NEWS.NEWSID = NEWS_TRANSLATION.NEWSID 
WHERE (NEWS.PUBLISH = 1) AND (NEWS_TRANSLATION.LANG = :lang) 
ORDER BY "NEWS"."NEWSID" DESC;
When are you are using "order by" and "rownum" together, you need to first order them and look for the first record.

http://stackoverflow.com/questions/11794242/oracle-select-with-subquery

No comments:

Post a Comment