Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:44:07+00:00 2026-05-26T06:44:07+00:00

This query returns forum questions/answers and their nested comments (similar to the StackOverflow paradigm).

  • 0

This query returns forum questions/answers and their nested comments (similar to the StackOverflow paradigm).

    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

I am trying to paginate the results and am hitting the following problem:

By placing LIMIT at the end of the query, I end up limiting the number of total rows, not the number of questions/answers.

For ex, LIMIT 0,3 in the last line gives me (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

instead of

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

Any suggestions how to modify my query in order to have LIMIT 0,3 return not 3 rows but 3 questions regardless of how many nested comments is has?

changes with @michael’s suggestion

    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

(HOPEFULLY) THE SOLUTION

After reading here

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

I decided to index the field I wanted to ORDER BY (forum_qa_type) — once I did this, the query is returning the correct number of questions.

Thanks @Michael for helping out. Will update here if this stops working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:44:08+00:00Added an answer on May 26, 2026 at 6:44 am

    Assuming forum_qa is the table holding the questions, you can place its SELECT * into a subquery with its own limit. This is of course untested, but should work in principle.

    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...
    

    Additional references to the table name forum_qa will need to be changed to its new alias qa.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
I have a LINQ query that returns some object like this... var query =
I'd like to make an (MS)SQL query that returns something like this: Col1 Col2
I'm calling the following URL using YQL http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22UTG.L%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=cbfunc this returns the following in JSON
how set distinct to this query return _dataContext.ProjectPictures.Include(Projects).Include(ProjectPictureTypes).Where(p => p.ProjectPictureTypes.Id == 1).ToList(); i need
How to make this method return boolean value, depending on query return. False -
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
This query is related to this one I asked yesterday. I have a radio
this query will just one row as a result myDataContext db = new myDataContext();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.