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

  • SEARCH
  • Home
  • 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 6208299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:47:09+00:00 2026-05-24T05:47:09+00:00

I use the following MySQL to return a list of posts and their corresponding

  • 0

I use the following MySQL to return a list of posts and their corresponding comments.

    SELECT  *
    FROM    forum_qa
            JOIN user_profiles
              ON user_id = forum_qa_author_id
            LEFT JOIN (SELECT forum_cm_id,
                              forum_cm_author_id,
                              forum_qa_id_fk,
                              forum_cm_text,
                        FROM  forum_cm
                        JOIN  user_profiles
                          ON  user_id = forum_cm_author_id) AS c
              ON forum_qa_id = c.forum_qa_id_fk
    WHERE   forum_qa_parent_id  = $forum_qa_id

If I run

    $data['num_answers'] = $query->num_rows();

This allows me to get the number of returned rows and pass the array to my controller and view.

But this is returning all rows (posts + comments). So if 1 post has 10 comments, it returns 10.

How could I have this query count only the number of posts (ie, returning 1) not including the subquery?

Each post has a unique id saved in forum_qa.forum_qa_id

Each comment has a unique id saved in forum_cm.forum_cm_id.

Thanks for helping — will post more code if needed.

  • 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-24T05:47:10+00:00Added an answer on May 24, 2026 at 5:47 am

    Not the fastest, but you are not restricted in using GROUP BY:

    SELECT  *, 
      (SELECT COUNT(*) FROM forum_qa WHERE forum_qa_parent_id  = $forum_qa_id) Cnt
        FROM    forum_qa
                JOIN user_profiles
                  ON user_id = forum_qa_author_id
                LEFT JOIN (SELECT forum_cm_id,
                                  forum_cm_author_id,
                                  forum_qa_id_fk,
                                  forum_cm_text,
                            FROM  forum_cm
                            JOIN  user_profiles
                              ON  user_id = forum_cm_author_id) AS c
                  ON forum_qa_id = c.forum_qa_id_fk
        WHERE   forum_qa_parent_id  = $forum_qa_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code for retrieve the data from mysql Student data Base.It
for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I have the following sample query (MySQL): SELECT * FROM `action` WHERE `customer_id` IN
Using MYSQL I would like to refactor the following SELECT statement to return the
Following query runs well in MySQL 5.x SELECT m_area.id, m_area.cn_areaName, m_area.de_areaName, m_area.en_areaName,m_area.jp_areaName,t_shop.count FROM m_area
Basically I have a mysql query something like the following: mysql_query(SELECT n.title, v.value FROM
I need to return all db-rows which match the following criteria: SELECT col FROM
I use the following mysql query, DELIMITER $$ DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$ CREATE
I use the following mysql procedure, DROP PROCEDURE IF EXISTS `allied`.`GetRegistrationData`$$ CREATE DEFINER=`allied`@`%` PROCEDURE
I use the following rsync command to backup my MySQL data to a machine

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.