Oracle选择子查询

时间:2012-08-03 10:49:54

标签: sql oracle subquery

你能帮我解决下一个oracle sql查询吗?

SELECT "NEWS"."NEWSID" as ID,
       "NEWS"."SLUG",
       "NEWS_TRANSLATION".*, 
       (SELECT FILENAME FROM NEWS_MEDIA WHERE NEWSID = ID AND rownum <= 1 ORDER BY POSITION ASC) 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;

当我执行此查询时,我有错误

ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:
Error at Line: 4 Column: 74

5 个答案:

答案 0 :(得分:10)

试试这个:

SELECT "NEWS"."NEWSID" as ID,
   "NEWS"."SLUG",
   "NEWS_TRANSLATION".*, 
   (SELECT * FROM (SELECT FILENAME FROM NEWS_MEDIA WHERE NEWSID = ID ORDER BY POSITION ASC) WHERE 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;

您何时一起使用“order by”和“rownum”,您需要先订购它们并查找第一条记录。

答案 1 :(得分:2)

以下是工作代码

SELECT "NEWS"."NEWSID" as ID,
   "NEWS"."SLUG",
   "NEWS_TRANSLATION".*, 
   (SELECT FILENAME FROM NEWS_MEDIA WHERE NEWSID = ID 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

答案 2 :(得分:1)

请尝试删除子查询中的order by子句。

答案 3 :(得分:1)

试试这个:

SELECT "NEWS"."NEWSID" AS ID, "NEWS"."SLUG", "NEWS_TRANSLATION".*,
       (SELECT FILENAME
          FROM NEWS_MEDIA
         WHERE NEWSID = "NEWS"."NEWSID" 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

答案 4 :(得分:-2)

SELECT STUDENT_NAME, STUDENT_ALIAS_NAME
FROM school
WHERE STUDENT_NAME IN (
        SELECT STUDENT_NAME
        FROM school
        WHERE STUDENT_DEPARMENT= 0
        );