use*_*789 26 sql oracle ora-00904
我想在一个查询中获得一篇帖子以及与该帖子相关的第一条评论.这是我在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中没有限制/偏移),我无法仅使用一个注释.
我在这做错了什么?
Qua*_*noi 47
不,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
| 归档时间: | 
 | 
| 查看次数: | 31570 次 | 
| 最近记录: |