Oracle.Query中的MySQL限制等价包括几个表?

时间:2011-06-09 16:27:52

标签: mysql oracle limit

我在MySQL中有以下查询,并希望将其转换为Oracle。我在Oracle中尝试了row_number()函数和子查询,但无法使其正常运行。该查询有点奇怪,涵盖多个表。

原始MySQL查询:

select DISTINCT uc.virtual_clip_id, uc.clip_id, uc.duration, uc.title,  
                uc.thumbnail,uc.filename, uc.description, uc.block_id_start, 
                uc.block_id_end, u.uname,uc.cdate, uc.ctime, uc.privacy_level, uc.user_id 
FROM
    user_clips uc, users u, user_like ul

WHERE 
     ul.user_id="+user_id+" and u.user_id=uc.user_id and  
     uc.virtual_clip_id=ul.virtual_clip_id and ul.like_status='1'
ORDER by 
      virtual_clip_id DESC
LIMIT "+offset+",4"

我无法找到替换LIMIT的正确语句,因此请对Oracle运行查询。

感谢。

3 个答案:

答案 0 :(得分:1)

这有点痛,但你可以使用rownum

select * from
(
select rownum as row_number, base_data.* from
(
select DISTINCT uc.virtual_clip_id, uc.clip_id, uc.duration, uc.title,
 uc.thumbnail,uc.filename, uc.description, uc.block_id_start, uc.block_id_end, u.uname,uc.cdate, uc.ctime, uc.privacy_level, uc.user_id FROM user_clips uc, users u, user_like ul

WHERE ul.user_id="+user_id+" and u.user_id=uc.user_id and
 uc.virtual_clip_id=ul.virtual_clip_id and ul.like_status='1' 
) base_data
ORDER by virtual_clip_id DESC
) sorted
WHERE row_number <= X

我选择两次的唯一原因是不同的是执行一个组而我保持在它之外的顺序,它可以用一个子选择来完成。

答案 1 :(得分:1)

select * from (
select DISTINCT
    uc.virtual_clip_id
  , uc.clip_id
  , uc.duration
  , uc.title
  , uc.thumbnail
  , uc.filename
  , uc.description
  , uc.block_id_start
  , uc.block_id_end
  , u.uname
  , uc.cdate
  , uc.ctime
  , uc.privacy_level
  , uc.user_id
  , row_number() over(order by uc.virtual_clip_id DESC) row_nr
   FROM
    user_clips uc
  , users u
  , user_like ul
  WHERE
    ul.user_id         = "+user_id+"
and u.user_id          = uc.user_id
and uc.virtual_clip_id = ul.virtual_clip_id
and ul.like_status     = '1'
)
 where row_nr < 5 ;

答案 2 :(得分:0)

select * 
from 
 (
  select DISTINCT uc.virtual_clip_id, uc.clip_id, uc.duration, uc.title,  
                  uc.thumbnail,uc.filename, uc.description, uc.block_id_start, 
                  uc.block_id_end, u.uname,uc.cdate, uc.ctime, uc.privacy_level, uc.user_id 
                , row_number() over(order by uc.virtual_clip_id desc) rn
  FROM
      user_clips uc, users u, user_like ul
  WHERE 
       ul.user_id="+user_id+" and u.user_id=uc.user_id and  
       uc.virtual_clip_id=ul.virtual_clip_id and ul.like_status='1'
 )
where rn between "+offset+" and 4