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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:38:10+00:00 2026-05-11T20:38:10+00:00

I have a hopefully simple MySQL query question which is eluding me at late

  • 0

I have a hopefully simple MySQL query question which is eluding me at late at night. I’m trying to do a SELECT which counts the number of instances of a set of data (orders) and groups those instances by a value which exists in a parent a couple levels above the order itself.

For example:

CREATE TABLE  `so_test`.`categories` (
  `id` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;

CREATE TABLE  `so_test`.`product_group` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `category_id` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;

CREATE TABLE  `so_test`.`products` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `product_group_id` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;

CREATE TABLE  `so_test`.`orders` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `product_id` int(10) unsigned NOT NULL auto_increment,
  `customer_id` int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=572395 DEFAULT CHARSET=latin1;

What I’m looking to do is something in the neighborhood of:

SELECT count(orders.id), categoryId
FROM orders, categories WHERE
orders.customer_id in (1,2,3) GROUP BY orders.productId.productGroupId.categoryId

Assuming there are 17 orders for products in category 1, 2 orders for products in category 2, and 214 orders for category 3, what I’m hoping to get back is:

count(orders.id), categoryId
============================
      17              1
      2               2
      214             3

If I was trying to group by say product_id I’d be fine..but the two-levels-up portion is throwing me.

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-05-11T20:38:10+00:00Added an answer on May 11, 2026 at 8:38 pm

    Just join them together:

    select categoryid, count(orders.id)
    from category c
    left join product_group pg on pg.category_id = c.id
    left join products on p on p.product_group_id = pg.id
    left join orders o on o.product_id = p.id
    

    For categories without an order, count(orders.id) will return 0, while count(*) would return one or more, depending on the number of productgroups and products.

    An inner join would not count categories without orders at all.

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

Sidebar

Related Questions

hopefully a pretty simple question this time. I have a Select method in a
I have a hopefully simple question... I am trying to change over from the
I have a hopefully simple question, but I have been trying to figure this
Hopefully a simple question here. I have the following html code: <select id=dropdown> <option
I have a simple question, which should hopefully have a quick answer. The code
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
I'm just getting started with Require.JS and I have a (hopefully) simple question. I'm
Hopefully a simple question although one I have found impossible to answer myself using
This is hopefully a simple question: I have an OpenGL texture and would like
I am trying to setup a simple database in which I have a user

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.