我用于获取分页总数的查询是:
SELECT *
FROM (SELECT r.*, ROWNUM RNUM, COUNT(*) OVER () RESULT_COUNT
FROM (SELECT t0.ABC_SEQ_ID AS c0, t0.FIRST_NAME, t1.SCORE
FROM ABC t0, XYZ t1
WHERE (t0.XYZ_ID = 751)
AND t0.XYZ_ID = t1.XYZ_ID
ORDER BY t0.RANK ASC) R)
WHERE RNUM between 1 and 15
显然,当分页值超出范围(大于数据大小)时,它不会返回任何行。问题是当分页值超出范围时,如何使用单个查询获得分页总计数?是否可以使用单个查询?
修改
例如:
特定条件的总行数为20.并且分页值为25到30.现在要将总行数发送给客户端,但此查询不会返回任何行。我该如何处理这个案子。
答案 0 :(得分:0)
WITH R AS
(SELECT r.*, ROWNUM RNUM, COUNT(*) OVER () RESULT_COUNT
FROM (SELECT t0.ABC_SEQ_ID AS c0, t0.FIRST_NAME, t1.SCORE
FROM ABC t0, XYZ t1
WHERE (t0.XYZ_ID = 751)
AND t0.XYZ_ID = t1.XYZ_ID
ORDER BY t0.RANK ASC
)
)
SELECT c0,FIRST_NAME,score,RESULT_COUNT FROM R WHERE RNUM between 25 and 30
UNION
SELECT null,null,null
,NVL((SELECT result_count FROM R WHERE ROWNUM=1),0) result_count
FROM DUAL WHERE NOT EXISTS(SELECT 1 FROM R WHERE RNUM between 25 and 30);
此查询未进行优化,但如果条件不匹配,它仍会给您计数,即0
答案 1 :(得分:0)
由于这是特殊的情况,如果需要,为什么不处理特殊?
保持查询不变,但如果没有返回任何行,请再次使用RNUM between 1 and 1
进行查询。
您可以为此使用相同的view / function / prepared语句,并且不会影响原始查询的性能。