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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:23:28+00:00 2026-05-27T18:23:28+00:00

I’m wondering all day and I cant get it done. I have a simple

  • 0

I’m wondering all day and I cant get it done.

I have a simple test table with news system as example. We have news, tags, and categories.
News can have many tags and one category. What I need is to count how many news are under each tag in each category. For example we could have 4 news with general tag under politics category and 2 news with general tag under science category.

My tables looks like this:

news:
    - news_id
    - category_id
    - title

categories:
    - category_id
    - category_name

tags:
    - tag_id
    - tag_name

news_tags:
    - news_id
    - tag_id

Here is a simple MindMap to clarify what I need:
enter image description here
https://i.stack.imgur.com/6ySiJ.png

Here is a query I’ve tried with no success:

SELECT *, COUNT(n.news_id) AS news_count FROM news AS n
LEFT JOIN categories AS c ON n.category_id = c.category_id
LEFT JOIN news_tags AS tn ON n.news_id = tn.news_id
LEFT JOIN tags AS t ON tn.tag_id = t.tag_id
GROUP BY t.tag_id, c.category_id;
  • 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-27T18:23:29+00:00Added an answer on May 27, 2026 at 6:23 pm

    Your query works without errors for me.
    What error do you get when you run your query/Why is it no success?

    This is my attempt to set up your situation:

    mysql> select * from news;
    +---------+-------------+------------------------+
    | news_id | category_ID | title                  |
    +---------+-------------+------------------------+
    |       1 |           1 | politics and general 1 |
    |       2 |           1 | politics and general 2 |
    |       3 |           1 | politics and general 3 |
    |       4 |           1 | politics and general 4 |
    |       5 |           2 | science and general 1  |
    |       6 |           2 | science and general 2  |
    |       7 |           2 | science and funny 1    |
    +---------+-------------+------------------------+
    
    mysql> select * from tags;
    +--------+----------+
    | tag_id | tag_name |
    +--------+----------+
    |      1 | general  |
    |      2 | funny    |
    +--------+----------+
    
    mysql> select * from news_tags;
    +---------+--------+
    | news_id | tag_id |
    +---------+--------+
    |       1 |      1 |
    |       2 |      1 |
    |       3 |      1 |
    |       4 |      1 |
    |       5 |      1 |
    |       6 |      1 |
    |       7 |      2 |
    +---------+--------+
    
    mysql> select * from categories;
    +-------------+---------------+
    | category_id | category_name |
    +-------------+---------------+
    |           1 | politics      |
    |           2 | science       |
    +-------------+---------------+
    

    Result of your query:

    +---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
    | news_id | category_ID | title                  | category_id | category_name | news_id | tag_id | tag_id | tag_name | news_count |
    +---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
    |       1 |           1 | politics and general 1 |           1 | politics      |       1 |      1 |      1 | general  |          4 |
    |       5 |           2 | science and general 1  |           2 | science       |       5 |      1 |      1 | general  |          2 |
    |       7 |           2 | science and funny 1    |           2 | science       |       7 |      2 |      2 | funny    |          1 |
    +---------+-------------+------------------------+-------------+---------------+---------+--------+--------+----------+------------+
    

    However it doesn’t make sense to SELECT * since you’re aggregating counts by tag/category and things like title don’t make sense when aggregated.

    You might try:

    SELECT c.category_name, t.tag_name, COUNT(n.news_id) AS news_count FROM news AS n
    LEFT JOIN categories AS c ON n.category_id = c.category_id
    LEFT JOIN news_tags AS tn ON n.news_id = tn.news_id
    LEFT JOIN tags AS t ON tn.tag_id = t.tag_id
    GROUP BY t.tag_id, c.category_id;
    

    To get:

    +---------------+----------+------------+
    | category_name | tag_name | news_count |
    +---------------+----------+------------+
    | politics      | general  |          4 |
    | science       | general  |          2 |
    | science       | funny    |          1 |
    +---------------+----------+------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.