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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:37:41+00:00 2026-06-01T11:37:41+00:00

I need help with a MySQL query. I have three tables: `product_category` ( `id`

  • 0

I need help with a MySQL query. I have three tables:

`product_category` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
PRIMARY KEY  (`id`)
);

`order_products` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `qty` int(11) NOT NULL,
  `unit_price` decimal(11,2) NOT NULL,
  `category` int(11) NOT NULL,
  `order_id` int (11) NOT NULL,
   PRIMARY KEY  (`id`)
);

`orders` (
  `id` int(11) NOT NULL auto_increment,
  `date` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
 );

I had a query that calculated subtotal of all orders by product category (in a certain date range). It looked like this:

 SELECT 
   SUM(op.unit_price * op.qty) as amt, 
   c.name as category_name 
 FROM 
   order_products op, 
   product_category c, 
   orders o 
 WHERE 
   op.category = c.id 
 AND 
   op.order_id = o.id
 AND
   o.date > 'xxxxxxx' 
 GROUP BY 
   c.id

This works great, but now I want to add the individual products and their subtotals to each row so that I get a result like this:

c.name|amt|op.name (1) - op.subtotal (1), op.name (2), op.subtotal (2), etc....

I figured out using GROUP_CONCAT that I could get the names to show up pretty easily by adding:

GROUP_CONCAT(op.name) as product_name

to the SELECT clause, but for the life of me I can’t figure out how to get the subtotal for each product to show up next to the product name. I have a feeling it involves a combination of joins or CONCAT nested inside GROUP_CONCAT, but nothing I’ve tried has worked. Any ideas?

@piotrm had an idea that seemed like it should work (below) but for some reason it returns the following:

SELECT  `subtotals` 
FROM  `product_category` 
WHERE  `c`.`category_name` =  'Fragrance'AND.`amt` =  '23164.50'AND.`subtotals` = CAST(     0x6c6f76656c696c7920454454202d203631302e30302c20466f72657665726c696c79202e313235206f7a2045445020726f6c6c657262616c6c202d20313831372e35302c20666f72657665726c696c79206361636865706f74202d2039302e30302c20666f72657665726c696c7920312f38206f756e63652070617266756d206f696c20726f6c6c657262616c6c202d20313833302e30302c20666f72657665726c696c792070657266756d696e6720626f6479206c6f74696f6e202d203938312e30302c20666f72657665726c696c7920332e34206f756e6365206561752064652070617266756d207370726179202009202d203535382e30302c20666f72657665726c696c79205363656e74656420566f746976652043616e646c65202d203132302e30302c20454450202620426f6f6b20736574202d203334332e30302c20666f72657665726c696c7920332e34206f756e6365206561752064652070617266756d207370726179202d2031363831352e3030 AS 
BINARY ) ;

As soon as I take out s.subtotal from the original SELECT clause it pulls the correct product names. The JOIN query pulls the products out correctly with their associated category_id and subtotals. I just can’t get the two to CONCAT together without creating this mess here. Any other thoughts?

Solution

@piotrm’s query was basically right, except GROUP_CONCAT is looking for a collection of strings. So the final query looked like this:

SELECT c.name AS category_name,
       SUM( s.subtotal ) AS amt,
       GROUP_CONCAT( CONCAT(s.name, ' - ', cast(s.subtotal as char) ) SEPARATOR ', ')                           AS subtotals
FROM 
       product_category c 
JOIN 
   (SELECT op.category, op.name, sum(op.qty*op.unit_price) AS subtotal
FROM order_products op
JOIN orders o ON o.id = op.order_id
WHERE o.date > '0'
GROUP BY op.category, op.name ) s 
ON s.category = c.id
GROUP BY c.name
  • 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-01T11:37:43+00:00Added an answer on June 1, 2026 at 11:37 am

    Guessing from your query there is also order_id field in your order_products table you didn’t mention in the table definition. Your query should then look like:

    SELECT c.name AS category_name,
                 SUM( s.subtotal ) AS amt,
                 GROUP_CONCAT( CONCAT(s.name, ' - ', s.subtotal ) SEPARATOR ', ' ) AS subtotals
    FROM 
        product_category c 
    JOIN 
      ( SELECT op.category, op.name, sum(op.qty*op.unit_price) AS subtotal
        FROM order_products op
        JOIN orders o ON o.id = op.order_id
        WHERE o.date > '2012-03-31'
        GROUP BY op.category, op.name ) s 
      ON s.category = c.id
    GROUP BY c.name
    

    Your db schema is quite weird though, orders table looks like it could be removed and that date moved to order_products, because for every order_products row you have reference to orders table. Usually it is the other way – there are many orders for every product referenced by product_id field in the orders table. Also date column in orders is of type varchar – why not date or datetime?

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

Sidebar

Related Questions

I need some help with a MySQL query. I have two tables, one with
I need help building a mysql query to select from multiple tables. I have
I am newbee with PHP and MySQL and need help... I have two tables
I need some help with a MySQL query I'm working on. I have data
I need some help with query from multiple tables. My database: I have following
I need help regarding array insertion into mysql table. I have an array when
I'm a noob with mysql and php, and need some help :) I have
I have three tables I need to use in a search, Movies, Reviews, and
I have three mysql tables, first holds information about articles (Article ID, author ID,
I need help in indexing in MySQL. I have a table in MySQL with

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.