获取从开始到结束数字之间的记录

get records between from a start to end number

这 SQL return 来自 2 个表“posts_main”和“posts_comments”的所有记录(JSON)取决于特定用户

$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_comments
from posts_main
WHERE posts_main.user_id = '$user_id' ";


$obj = json_decode($_GET["x"], false);
$stmt = $con->prepare($sql);
$stmt->bind_param("ss", $obj->table, $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);

echo json_encode($outp);

我的问题: 如何获取前 10 条记录,然后是接下来的 10 条记录,直到最后?

例如: 我会发送 "start" = 0,我会得到前 10 条记录。 下次我会发送:"start" = 10,然后我会得到记录 FROM 10 TO 20。 等等。

谢谢...

如果您有一个 "start" 号码,您可以像这样构建您的查询 -

$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_comments
from posts_main
WHERE posts_main.user_id = '$user_id' limit 10,10";

MySQL 从 table 中的第 11 条记录开始读取 10 条记录。 如果您正在寻找 Prev, Next 解决方案,那么 PHP 分页是唯一的选择。

希望对您有所帮助:

$start = 0;
$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_comments
from posts_main
WHERE posts_main.user_id = '$user_id' 
limit '$start' 10";