2017-07-28 85 views
0

在这个SQL查询中有25条记录 我想得到5到10之间。我该怎么做? 我用11克Oracle数据库Rownum之间

select 
(
    select count(*) as sayfasayisi 
    from konular t 
    where t.kategori is not null 
) as sayfasayisi, 
t.id, 
t.uye, 
t.baslik,t.mesaj,t.kategori,t.tarih, 
t.edittarih,t.aktif,t.indirimpuani,t.altkategori,t.link, 
nvl(
    (select case when t.id = f.konuid and f.uye = 'test' then '1' else '0' end 
    from takipkonu f where t.id = f.konuid and f.uye = 'test'), '0') as takip 
from konular t 
where t.kategori is not null 
+0

Oracle不会为记录分配永久行号,所以如果您要查找表中的第5到第10条记录,请知道它不能保证你每次都会得到相同的记录。对于您的具体问题,您需要查看您仅检索这些记录的具体要求。 – Incognito

+0

您可以使用'ROW_NUMBER()'进行一定的排序,然后只保留第5到第10条记录。 [见这里](https://stackoverflow.com/questions/7480243/sql-oracle-order-by-and-limit)。 –

回答

0

您可以使用ROW_NUMBER()基于包含在当前的查询,例如一些排序逻辑分配行号某一列。然后,只保留第5到第10条记录:

select t.* 
from 
(
    select 
    (
     select count(*) as sayfasayisi 
     from konular t 
     where t.kategori is not null 
    ) as sayfasayisi, 
    ROW_NUMBER() OVER (ORDER BY some_col) rn, 
    t.id, 
    t.uye, 
    ... 
) t 
where t.rn between 5 and 10;