无法使用LIMIT OFFSET访问较低的行

时间:2014-03-01 17:50:41

标签: php mysql select join count

我的表格结构如下。

users
ID      name

comments
ID      post_id     comment

product
pid     views   post_id     filesize    upload_date

product_group
gid     views   post_id         group_category

product_category
cid     views   post_id

我想让所有用户(即使是那些没有上传任何产品的用户)使用他们上传的产品编号,按照他们上传的产品编号排序。我还需要获得评论数量,用户所有产品的观看次数,上次上传日期等。

我接受了以下查询。

    (SELECT COUNT(product.pid) AS p_num,
            SUM(product.views) AS views,
            COUNT(comments.ID) AS comment,
            SUM(product.filesize) AS size,
            MAX(product.upload_date) AS date,
            users.*
    FROM users
    LEFT JOIN product ON users.ID = product.uploader AND exclude =0
    LEFT JOIN product_group ON product_group.gid = product.groupid AND product_group.group_category =0
    LEFT JOIN comments ON product.post_id = comments.ID
    WHERE product_group.gid IS NULL AND users.ID = "Current user id")
    UNION
    (SELECT COUNT(product.pid) AS p_num,
            SUM(product.views) AS views,
            COUNT(comments.ID) AS comment,
            SUM(product.filesize) AS size,
            MAX(product.upload_date) AS date,
            users.* FROM
            users
    LEFT JOIN product ON users.ID = uploader AND exclude =0
    LEFT JOIN product_group ON gid = product.groupid AND product_group.group_category =0
    LEFT JOIN comments ON product.post_id = comments.ID
    WHERE product_group.gid IS NULL
    GROUP BY users.ID
    ORDER BY p_num DESC
    LIMIT 24 OFFSET 0)

第一个选择是将当前用户保持在所有页面的顶部。

如果我不提供任何LIMIT OFFSET子句,查询将完美运行。

它返回964行,即我数据库中用户的总数。我已经检查了评论,upload_date。这些也正确返回。

但是,如果我使用LIMIT 10 OFFSET 950尝试相同的查询,则返回空结果集。 它似乎以这种方式返回947行。我不明白这里发生了什么。

0 个答案:

没有答案