MySQL one to many relationship: GROUP_CONCAT or JOIN or both?

Go To StackoverFlow.com

1

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
2012-04-05 19:05
by Matthew Beaumont


0

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?

2012-04-05 21:27
by piotrm
order_products stores the products associated with an order (one to many relationship, because an order can have many products). Date column is varchar because I store unix timestamp there, not datetime. Your answer seems like it should work, I'll amend the original post to show what happens - Matthew Beaumont 2012-04-05 22:25
Got it! Your SQL was right on the money, but GROUP_CONCAT won't work on a dynamic column. I'll post the solution in the original post, but I'm marking your answer as correct, because I wouldn't have gotten there without it. Thanks - Matthew Beaumont 2012-04-06 01:35


0

Try:

SELECT 
    SUM(op.unit_price * op.qty) as amt, 
    c.name as category_name,
    group_concat(concat(op.name, '-', op.qty, ',' , 
                 op.unit_price*op.qty) separator '|')
    ...
2012-04-05 19:52
by Maksym Polshcha
Thanks - I had tried that already, but it doesn't work. It returns a "blob" in that column with the following data: code SELECT subtotal FROM order_products WHERE.amt = '##.###' AND c.category_name = '[Category Name]' - Matthew Beaumont 2012-04-05 20:06
btw you should group by both c.id and c.nam - Maksym Polshcha 2012-04-05 20:13
Unfortunately that didn't do it either. Thanks - Matthew Beaumont 2012-04-05 20:42
Ads