sql – Oracle子查询没有看到外部块2级别的变量

我想在一个查询中收到一篇帖子以及与帖子相关的第一条评论.以下是我在Postgresql中的表现:
SELECT p.post_id,(select * from 
 (select comment_body from comments where post_id = p.post_id 
 order by created_date asc) where rownum=1
) the_first_comment
FROM posts p

它工作正常.

但是,在Oracle中我收到错误ORA-00904 p.post_id:无效的标识符.

对于一个子选择似乎工作正常,但由于我需要使用rownum(Oracle中没有限制/偏移),我无法仅使用一个注释.

在这做错了什么?

解决方法

不,Oracle不会将嵌套多个子级别的子查询关联起来(MysqL也不会).

这是一个众所周知的问题.

用这个:

SELECT  p.post_id,c.*
FROM    posts
JOIN    (
        SELECT  c.*,ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY created_date ASC) AS rn
        FROM    comments c
        ) c
ON      c.post_id = p.post_id
        AND rn = 1

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...