我正在研究Oracle / PHP应用程序并尝试实现“加载更多”功能。
以下是两个表及其结构:
questions:
id (int) | title (varchar(999)) | datetime (varchar(999))
questions_tags:
id (int) | question_id (int) | tag_id (int)
以下是index.php中的代码:
$stid = oci_parse($conn, "select * from (
select a.*, ROWNUM rnum from (
SELECT questions.id FROM questions_tags, questions WHERE
questions.id = questions_tags.question_id AND
questions_tags.tag_id IN (1,2,3,4,5,6,7) ORDER BY questions.datetime ASC
) a where rownum <= 10
) where rnum >= 0");
oci_execute($stid);
while (oci_fetch($stid)) {
// Do work with the results
}
以下是它背后的javascript:
$("#load_more").click(function(e) {
$.ajax({
url: "loadMore.php?x=" + $(".span_data:last").data("id"),
success:function(data, textStatus, jqXHR)
{
if (data)
{
// append data
} else {
// remove button
}
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
e.preventDefault(); //STOP default action
});
最后,这是loadMore.php文件:
<?php
$x = $_GET['x'];
$xTen = intval($x) + 10;
$count_id = $x;
try
{
$lastID = 0;
$tagIDs = implode("','", $user_tag_ids);
$stid = oci_parse($conn, "select * from (
select a.*, ROWNUM rnum from (
SELECT questions.id FROM questions_tags, questions WHERE
questions.id = questions_tags.question_id AND
questions_tags.tag_id IN (1,2,3,4,5,6,7) ORDER by questions.datetime DESC
) a where rownum <= $xTen
) where rnum >= $x");
oci_execute($stid);
while (oci_fetch($stid)) {
// Do work with the results
}
}
catch(Exception $e) {
print "Error";
}
?>
无论x
的值是什么,loadMore.php
文件都会继续返回相同的结果集。因此,如果我执行/loadMore.php?x=10
,我将得到与/loadMore.php?x=100
返回的结果相同的结果。但是,如果我更进一步(/loadMore.php?x=1500
),那么当我最终得到不同的结果时。我无法理解这里出了什么问题。
正如我在评论中所说:
如果我删除了所有rownum
参数,只需运行
SELECT * FROM questions WHERE EXISTS (
SELECT 1 FROM questions_tags WHERE
questions.id = questions_tags.question_id AND
questions_tags.tag_id IN (1,2,3,4,5,6,7)
) ORDER BY datetime DESC
我得到了所有结果,可能是一千行。我试图获得这千行中的前10行,然后在用户单击“加载更多”按钮时继续获得下一行。这就是为什么我有相同的查询index.php和loadMore.php
答案 0 :(得分:1)
感谢评论,我意识到ORDER BY questions.datetime
不够独特,无法正确排序结果。将查询更改为ORDER BY questions.datetime DESC, questions.id DESC
修复了问题,并开始显示独特的结果。