MySQL选择" n" rows和onButtonClick选择next" n"行

时间:2017-11-20 11:43:36

标签: php mysql

我希望我的书名是正确的。

我的想法,我想要实现的目标:

我有(基于Instagram的布局与帖子(图像))。在页面加载时,我想选择" n"行(例如50)。在那50行后我想显示按钮(加载更多)。如果我单击此按钮,我想加载下50行。因此,每50行(后期计数可能以百万计)。

我不想加载所有数据并在页面中制作,因为

  1. 我想实现这一点,以保存互联网数据。
  2. 加载所有帖子需要很长时间。
  3. 这是我从mysql中选择数据的方式(此时):

    SELECT * 
    FROM fun_posts fp 
    JOIN ( 
        SELECT image_id, COUNT(*) AS upvotes 
        FROM fun_post_upvotes 
        GROUP BY image_id 
        ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC, 
        count(*) DESC, 
        date DESC ) fpu 
    ON ( fpu.image_id = fp.id )
    

4 个答案:

答案 0 :(得分:3)

让我们根据您的要求考虑您的问题,

然后以下代码将帮助您,

SELECT * 
FROM fun_posts fp 
JOIN ( 
    SELECT image_id, COUNT(*) AS upvotes 
    FROM fun_post_upvotes 
    GROUP BY image_id 
    ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC, 
    count(*) DESC, 
    date DESC ) fpu 
ON ( fpu.image_id = fp.id )
limit START_ROW,EXPECTED_NO_OF_ROW

you required this in lazy loading so your variables should be change on each load, 
Example : Lets consider you want to show 50 rows then,
1st load : START_ROW = 0, EXPECTED_NO_OF_ROW = 50 
2nd load : START_ROW = 50, EXPECTED_NO_OF_ROW = 50 
3rd load : START_ROW = 100, EXPECTED_NO_OF_ROW = 50 so on....

答案 1 :(得分:0)

您可能希望在示例页面上使用LIMIT作为:

  

SELECT * FROM tbl LIMIT 5,10; #检索行6-15

由于您要确保使用顺序列表,因此您希望它在派生连接表中。您的限制将始终为50(您想要的帖子数量),您的偏移量需要计算为50*(page-1),因此第1页为LIMIT 0, 50,第2页为LIMIT 50, 50... < / p>

SELECT * 
FROM fun_posts fp 
JOIN ( 
    SELECT image_id, COUNT(*) AS upvotes 
    FROM fun_post_upvotes 
    GROUP BY image_id 
    ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC, 
    count(*) DESC, 
    date DESC
    LIMIT x, 50 ) fpu -- Where is the offset that is calculated 
ON ( fpu.image_id = fp.id )

答案 2 :(得分:0)

传递动态偏移并限制从​​php到mysql

首先设置limit = 50;

然后首先设置offset = 0;比每次在offset中添加limit一样

offset = offset + limit;

答案 3 :(得分:0)

向服务器提交名为&#34; page&#34;的param;或类似的东西,然后使用OFFSET和LIMIT。

客户端示例:

...
    <a href="url?page=1">Page 1</a>
    <a href="url?page=2">Page 2</a>
...

服务器脚本示例:

<?php
$serverPagination = 50; // you can modify the number of rows per page
$serverPage = isset($_GET['page']) ? $_GET['page'] - 1 : 0; // for offset
$offset = $pagination * $serverPage;

$sql = "
SELECT * 
FROM fun_posts fp 
JOIN ( 
    SELECT image_id, COUNT(*) AS upvotes 
    FROM fun_post_upvotes 
    GROUP BY image_id 
    ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC, 
    count(*) DESC, 
    date DESC ) fpu 
ON ( fpu.image_id = fp.id ) LIMIT {$serverPagination} OFFSET {$offset}";

// excution of query
?>