需要帮助将LIMIT,OFFSET放在此MySQL查询中

pep*_*epe 2 mysql sql limit offset

此查询返回论坛问题/答案及其嵌套注释(类似于StackOverflow范例).

    SELECT forum_qa.*,
           user_profiles.*,
           c.*,
           n.pid,
           v.*,
           Ifnull(n.ans_count, 0) AS ans_count
    FROM   forum_qa
           JOIN user_profiles
             ON user_id = forum_qa_author_id
           LEFT JOIN (SELECT *
                      FROM   votes) AS v
             ON forum_qa_id = v.forum_qa_id_fk
           LEFT JOIN (SELECT forum_cm_id,
                             forum_cm_author_id,
                             forum_qa_id_fk,
                             forum_cm_text,
                             forum_cm_timestamp,
                             forum_cm_flag,
                             first_name  AS forum_cm_first_name,
                             last_name   AS forum_cm_last_name,
                             facebook_id AS forum_cm_fb_id,
                             picture     AS forum_cm_picture,
                             moderator   AS forum_cm_moderator
                      FROM   forum_cm
                             JOIN user_profiles
                               ON user_id = forum_cm_author_id) AS c
             ON forum_qa_id = c.forum_qa_id_fk
           LEFT JOIN (SELECT forum_qa_parent_id AS pid,
                             COUNT(*)           AS ans_count
                      FROM   forum_qa
                      WHERE  forum_qa_parent_id IS NOT NULL
                      GROUP  BY forum_qa_parent_id) AS n
             ON forum_qa_id = n.pid
    WHERE  forum_qa_id LIKE "%"
           AND forum_qa_parent_id IS NULL
    ORDER  BY forum_qa_timestamp DESC
            LIMIT  0,3
Run Code Online (Sandbox Code Playgroud)

我试图对结果进行分页,并遇到以下问题:

通过在查询的结尾将LIMIT,我结束了限制的人数行,没有问题/答案的数目.

对于前者,LIMIT 0,3在最后一行给我(qa:question/answer; cm:comment):

forum_qa_id   qa_text   forum_cm_id    cm_text
1             asd
2             wer       4              this is a comment
2             wer       5              this is another comment
Run Code Online (Sandbox Code Playgroud)

代替

forum_qa_id   qa_text   forum_cm_id    cm_text
1             asd
2             wer       4              this is a comment
2             wer       5              this is another comment
3             zxc       
3             zxc       7              yet another comment
Run Code Online (Sandbox Code Playgroud)

任何建议如何修改我的查询,以便LIMIT 0,3返回不是3行但3个问题,无论有多少嵌套注释?

随着@ michael的建议而改变

    SELECT qa.*,
           user_profiles.*,
           c.*,
           n.pid,
           v.*,
           Ifnull(n.ans_count, 0) AS ans_count
    FROM   (SELECT * FROM forum_qa LIMIT 0, 3) qa
           JOIN user_profiles
             ON user_id = qa.forum_qa_author_id
           LEFT JOIN (SELECT *
                      FROM   votes) AS v
             ON qa.forum_qa_id = v.forum_qa_id_fk
           LEFT JOIN (SELECT forum_cm_id,
                             forum_cm_author_id,
                             forum_qa_id_fk,
                             forum_cm_text,
                             forum_cm_timestamp,
                             forum_cm_flag,
                             first_name  AS forum_cm_first_name,
                             last_name   AS forum_cm_last_name,
                             facebook_id AS forum_cm_fb_id,
                             picture     AS forum_cm_picture,
                             moderator   AS forum_cm_moderator
                      FROM   forum_cm
                             JOIN user_profiles
                               ON user_id = forum_cm_author_id) AS c
             ON qa.forum_qa_id = c.forum_qa_id_fk
           LEFT JOIN (SELECT forum_qa_parent_id AS pid,
                             COUNT(*)           AS ans_count
                      FROM   forum_qa
                      WHERE  forum_qa_parent_id IS NOT NULL
                      GROUP  BY forum_qa_parent_id) AS n
             ON qa.forum_qa_id = n.pid
    WHERE  qa.forum_qa_id LIKE "%"
           AND qa.forum_qa_parent_id IS NULL
    ORDER  BY qa.forum_qa_timestamp DESC
Run Code Online (Sandbox Code Playgroud)

(HOPEFULLY)解决方案

看完这里

http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/

我决定索引我想要的字段ORDER BY(forum_qa_type) - 一旦我这样做,查询返回正确数量的问题.

谢谢@Michael帮忙.如果停止工作,将在此处更新.

Mic*_*ski 5

假设forum_qa表中包含问题,您可以将其SELECT *放入具有自己限制的子查询中.这当然是未经测试的,但原则上应该有效.

SELECT     qa.*,
           user_profiles.*,
           c.*,
           n.pid,
           v.*,
           Ifnull(n.ans_count, 0) AS ans_count
    FROM   (SELECT * FROM forum_qa LIMIT 0, 3) qa 
          JOIN user_profiles
             ON user_profiles.user_id = qa.forum_qa_author_id
           LEFT JOIN (SELECT *.....
           -- etc...
Run Code Online (Sandbox Code Playgroud)

对表名的其他引用forum_qa将需要更改为其新别名qa.