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 8555653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:10:23+00:00 2026-06-11T15:10:23+00:00

. I have 4 tables as shown above. Now I’m trying to get all

  • 0

database tables
.

I have 4 tables as shown above. Now I’m trying to get all the data including article subjects, tags for each article, and number of comments for each article, in ONE query.

The sql query I’m using now is

SELECT
    articles.subject, GROUP_CONCAT(tags.name) AS tags, COUNT(comments.aid) AS comments
FROM articles
    LEFT JOIN comments ON comments.aid = articles.aid
    LEFT JOIN relations ON relations.aid = articles.aid
    LEFT JOIN tags ON tags.tid = relations.tid
GROUP BY
    articles.aid

The result: data in [ ] is what I actually get

array
(
    1 => array
    (
        subject => foo
        tags =>
        comments => 1
    )
    2 => array
    (
        subject => bar
        tags => html,mysql [html,mysql,html,mysql]
        comments => 2 [4]
    )
    3 => array
    (
        subject => baz
        tags => php
        comments => 0
    )
)

For the real situation here in my application, number of tags and number of comments will multiply with each other. For example: if there are 4 comments, and 3 tags in one article, my query will result in

tags: html,css,php, html,css,php, html,css,php, html,css,php (instead of html,css,php)

comments: 12 (instead of 4)

I know there must be something wrong with my query statement, I just don’t know how to fix it.
Someone please help. Thanks.

  • 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-06-11T15:10:24+00:00Added an answer on June 11, 2026 at 3:10 pm

    I think you need a nested query to count the comments

    SELECT
        articles.subject, GROUP_CONCAT(tags.tag) AS tags, comments
    FROM articles
        LEFT JOIN (
              select aid,count(cid) as comments from comments group by aid
        ) AS commentscount ON commentscount.aid = articles.aid
        LEFT JOIN relations ON relations.aid = articles.aid
        LEFT JOIN tags ON tags.tid = relations.tid
    GROUP BY
        articles.aid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables as shown here . I am trying to fetch name
I have three tables as shown in below image. Note: Lead column of projectheader
I have two tables as shown in the following diagram : as you saw
I have created two tables & inserted values as shown below . Table 1
I do have a table data as shown below: <td> <label for=title>Title : </label>
I have two Tables Let suppose A and B Now suppose the structure of
I have 3 tables; Inbox, Outbox, and Messages. For a single message, the data
7 tables. In each table there is a common entity called fisherid. I have
I have a grid view utilizing sql data source. Now I want to delete
I have two tables: PlanMaster (PlanName, Product_ID) and ProductPoints (Entity_ID, Product_ID, Comm1, Comm2) Now

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.