MySQL一对多的关系:GROUP_CONCAT或JOIN或两者兼而有之?

时间:2012-04-05 19:05:50

标签: mysql

我需要MySQL查询的帮助。我有三张桌子:

`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`)
 );

我有一个查询按产品类别(在特定日期范围内)计算所有订单的小计。它看起来像这样:

 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

这很好用,但现在我想将每个产品及其小计添加到每一行,以便得到如下结果:

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

我发现使用GROUP_CONCAT可以通过添加以下内容轻松显示名称:

GROUP_CONCAT(op.name) as product_name

到SELECT子句,但就我而言,我无法弄清楚如何在产品名称旁边显示每个产品的小计。我有一种感觉,它涉及到GROUP_CONCAT内嵌的连接或CONCAT的组合,但我尝试过的任何工作都没有。有什么想法吗?

@piotrm有一个看起来应该有用的想法(下面)但是由于某种原因它会返回以下内容:

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

一旦从原始SELECT子句中取出s.subtotal,它就会提取正确的产品名称。 JOIN查询使用关联的category_id和小计正确地拉出产品。我不能把这两个人带到CONCAT,而不会在这里造成这种混乱。还有其他想法吗?

解决方案

@ piotrm的查询基本上是正确的,除了GROUP_CONCAT正在寻找一组字符串。所以最终的查询看起来像这样:

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

2 个答案:

答案 0 :(得分:0)

尝试:

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 '|')
    ...

答案 1 :(得分:0)

根据您的查询猜测,您的order_id表中还有order_products字段,您未在表定义中提及。您的查询应如下所示:

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

您的数据库架构非常奇怪,订单表看起来可以删除,并且该日期已移至order_products,因为对于每个order_products行,您都引用了orders表。通常情况是另一种方式 - 订单表中product_id字段引用的每个产品都有许多订单。订单中的日期列也是varchar类型 - 为什么不是日期或日期时间?