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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:22:53+00:00 2026-05-23T11:22:53+00:00

I have so many problems trying to understand this, so forgive me for what’s

  • 0

I have so many problems trying to understand this, so forgive me for what’s most likely a repeat question.

I have 2 tables, one called “category” and one called “items”. Each row in “items” has a category_id field that matches the category_id field in the “category” table.

In one mysql statement, I want to get a row count for each category_id in “items”, but also retrieve the category_name in “category”, so I can print out a list of categories like this:

  • Foo (3)
  • Bar (1)
  • Duh (6)

The questions I’ve found here so far only seem to return the category_id’s from “items”, so the result would look like this:

  • 1 (3)
  • 2 (1)
  • 3 (6)

Follow? I’m sure it can be done in one mysql statement, so far I’ve had to resort to two separate queries for the sole purpose of getting that little number.

  • 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-23T11:22:53+00:00Added an answer on May 23, 2026 at 11:22 am

    This should work for you;

    SELECT c.name AS theName, COUNT(i.id) AS theCount
    FROM category AS c JOIN items AS i ON i.category_id = c.id
    GROUP BY i.category_id
    

    //Edit

    Sorry I should then explain this.

    Select Is grabbing the category name and the count of the items.

    From Is doing a join that will bind items and categories together.

    Group By Is ensuring that the Category ID is the unique thing in the query.

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

Sidebar

Related Questions

Firstly, please forgive me if I have asked this before (i suffer memory problems
sorry if the question is too dull but I have been trying to understand
I understand there are many threads like mine but I have been trying to
I have worked many projects with no problems. But in current project, I tested
This sounds like a problem many others have posted about, but there's a nuance
I have faced the same problem many times. The Same Problem was With This
I'm sure this is the kind of problem other have solved many times before.
I've been working on this problem for a few hours and have used many
I have seen this problem arise in many different circumstances and would like to
This problem has bugged me so many times and i have now decided to

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.