7

你能帮我下一个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
4

5 回答 5

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”,你需要先对它们排序并查找第一条记录。

于 2012-08-03T17:56:51.473 回答
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
于 2012-08-03T13:08:11.547 回答
1

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

于 2012-08-03T12:19:07.563 回答
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
于 2012-08-03T13:17:35.847 回答
-2
SELECT STUDENT_NAME, STUDENT_ALIAS_NAME
FROM school
WHERE STUDENT_NAME IN (
        SELECT STUDENT_NAME
        FROM school
        WHERE STUDENT_DEPARMENT= 0
        ); 
于 2014-07-10T06:08:04.563 回答