简体   繁体   中英

Oracle sql missing right parenthesis

I'm trying to limit the number of rows that would be displayed when I run my query. When I run the code below in SQL developer, it returns missing right parenthesis error..

select * from
(select row_number()  over (order by rescode) rnum, a.* from
    (    
     SELECT * 
     FROM trans z 
     LEFT JOIN emails a 
     ON z.email1_hist_id=a.email_id 
     or z.email2_hist_id=a.email_id 
    ) a
)   where rnum between 1 and 50;

I tried running the inside query:

SELECT * 
FROM trans z 
LEFT JOIN emails a 
ON z.email1_hist_id=a.email_id 
or z.email2_hist_id=a.email_id

and it works fine. On the other hand I tried removing the OR portion of my query and included the limit rows query and it returns the number of rows I specified.

What exactly is wrong in my code?

This should work - you don't need two levels of subquery

select *
from
(    SELECT *, row_number()  over (order by rescode) rnum
     FROM trans z 
     LEFT JOIN emails a 
     ON (z.email1_hist_id=a.email_id or z.email2_hist_id=a.email_id)
) x
where rnum between 1 and 50;

Also, make sure there are no duplicate column names between trans and emails - this will trip the query because * from the inner query cannot return duplicate names.

我最好的猜测是,它不喜欢您给子选择一个别名,因此它在“)a”中的“ a”处引发了语法错误。

I don't recall about Oracle, but I know that MySQL actually requires sub-selections to have an alias. I'd try adding one to your outer sub-select (before the where rnum...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM