MySQL - 聚合和 GROUP BY 子查询
MySQL - aggregating and GROUP BY with subqueries
是否可以使用 GROUP BY 来聚合带有子查询的单元格?我正在使用的数据库包含客户订单信息(订单 ID、名称、地址、产品、选项等)。当客户为产品选择多个选项时,它们会显示在不同的行中。我需要通过将所有产品选项映射到新列来将它们聚合到同一行,但不确定在哪里添加 GROUP BY,因为我有子查询...
查询如下:
http://sqlfiddle.com/#!9/83a399/4/0
预期输出:
Order ID | Qty | Option | Size | Product | Ref
-----------------------------------------------
12345 | 1 | R | L | Tee | R / Tee L
12346 | 2 | Bl | S | Hood | 2x Bl / Hood S
GROUP BY 具有唯一键的列和其余的 zse aggregation functions就像您的情况下的 MAX
SELECT
`Order ID`,
`Qty`,
MAX(`Option`),
MAX(`Size`),
`Product`,
MAX(CONCAT (
CASE
WHEN `Qty` > 1
THEN CONCAT (`Qty`, 'x ')
ELSE ''
END,
`Option`,
' / ',
`Product`,
CASE
WHEN COALESCE (`Size`, '') != ''
THEN CONCAT (' ', `Size`)
ELSE ''
END
))
AS `Ref`
FROM (
SELECT
`order`.`order_id` AS `Order ID`,
`order_product`.`quantity` AS `Qty`,
CASE
WHEN `order_option`.`value` LIKE '%Red%' THEN 'R'
WHEN `order_option`.`value` LIKE '%Blue%' THEN 'Bl'
END AS `Option`,
CASE
WHEN `order_product`.`name` LIKE '%t-shirt%' THEN 'Tee'
WHEN `order_product`.`name` LIKE '%hoodie%' THEN 'Hood'
ELSE `order_product`.`name`
END AS `Product`,
CASE
WHEN `order_option`.`value` LIKE '%Small%' THEN 'S'
WHEN `order_option`.`value` LIKE '%Medium%' THEN 'M'
WHEN `order_option`.`value` LIKE '%Large%' THEN 'L'
ELSE ''
END AS `Size`
FROM
`order`,
`order_product`,
`order_option`
WHERE
`order`.`order_id` = `order_option`.`order_id` AND
`order_product`.`order_product_id` = `order_option`.`order_product_id`
ORDER BY `order`.`order_id` ASC
) AS `Orders`
GROUP BY
`Order ID`,
`Qty`,
`Product` ;
您必须 group by Order ID
并汇总:
SELECT
`Order ID`,
MAX(`Qty`) AS `Qty`,
MAX(`Option`) AS `Option`,
MAX(`Size`) AS `Size`,
MAX(`Product`) AS `Product`,
CONCAT_WS(' ',
MAX(CONCAT(
CASE WHEN `Qty` > 1 THEN CONCAT(`Qty`, 'x ') ELSE '' END,
`Option`, ' / ', `Product`,
CASE WHEN COALESCE(`Size`, '') != '' THEN CONCAT(' ', `Size`) ELSE '' END
)),
MAX(`Size`)
) AS `Ref`
FROM (
SELECT
o.`order_id` AS `Order ID`,
op.`quantity` AS `Qty`,
CASE
WHEN oo.`value` LIKE '%Red%' THEN 'R'
WHEN oo.`value` LIKE '%Blue%' THEN 'Bl'
END AS `Option`,
CASE
WHEN op.`name` LIKE '%t-shirt%' THEN 'Tee'
WHEN op.`name` LIKE '%hoodie%' THEN 'Hood'
ELSE op.`name`
END AS `Product`,
CASE
WHEN oo.`value` LIKE '%Small%' THEN 'S'
WHEN oo.`value` LIKE '%Medium%' THEN 'M'
WHEN oo.`value` LIKE '%Large%' THEN 'L'
ELSE ''
END AS `Size`
FROM `order` AS o
INNER JOIN `order_option` AS oo ON o.`order_id` = oo.`order_id`
INNER JOIN `order_product` AS op ON op.`order_product_id` = oo.`order_product_id`
) AS `Orders`
GROUP BY `Order ID`
ORDER BY `Order ID` DESC
我对您的代码进行了更改,例如对表使用适当的连接和别名。
见 demo.
结果:
| Order ID | Qty | Option | Size | Product | Ref |
| -------- | --- | ------ | ---- | ------- | -------------- |
| 12346 | 2 | Bl | S | Hood | 2x Bl / Hood S |
| 12345 | 1 | R | L | Tee | R / Tee L |
是否可以使用 GROUP BY 来聚合带有子查询的单元格?我正在使用的数据库包含客户订单信息(订单 ID、名称、地址、产品、选项等)。当客户为产品选择多个选项时,它们会显示在不同的行中。我需要通过将所有产品选项映射到新列来将它们聚合到同一行,但不确定在哪里添加 GROUP BY,因为我有子查询...
查询如下: http://sqlfiddle.com/#!9/83a399/4/0
预期输出:
Order ID | Qty | Option | Size | Product | Ref
-----------------------------------------------
12345 | 1 | R | L | Tee | R / Tee L
12346 | 2 | Bl | S | Hood | 2x Bl / Hood S
GROUP BY 具有唯一键的列和其余的 zse aggregation functions就像您的情况下的 MAX
SELECT
`Order ID`,
`Qty`,
MAX(`Option`),
MAX(`Size`),
`Product`,
MAX(CONCAT (
CASE
WHEN `Qty` > 1
THEN CONCAT (`Qty`, 'x ')
ELSE ''
END,
`Option`,
' / ',
`Product`,
CASE
WHEN COALESCE (`Size`, '') != ''
THEN CONCAT (' ', `Size`)
ELSE ''
END
))
AS `Ref`
FROM (
SELECT
`order`.`order_id` AS `Order ID`,
`order_product`.`quantity` AS `Qty`,
CASE
WHEN `order_option`.`value` LIKE '%Red%' THEN 'R'
WHEN `order_option`.`value` LIKE '%Blue%' THEN 'Bl'
END AS `Option`,
CASE
WHEN `order_product`.`name` LIKE '%t-shirt%' THEN 'Tee'
WHEN `order_product`.`name` LIKE '%hoodie%' THEN 'Hood'
ELSE `order_product`.`name`
END AS `Product`,
CASE
WHEN `order_option`.`value` LIKE '%Small%' THEN 'S'
WHEN `order_option`.`value` LIKE '%Medium%' THEN 'M'
WHEN `order_option`.`value` LIKE '%Large%' THEN 'L'
ELSE ''
END AS `Size`
FROM
`order`,
`order_product`,
`order_option`
WHERE
`order`.`order_id` = `order_option`.`order_id` AND
`order_product`.`order_product_id` = `order_option`.`order_product_id`
ORDER BY `order`.`order_id` ASC
) AS `Orders`
GROUP BY
`Order ID`,
`Qty`,
`Product` ;
您必须 group by Order ID
并汇总:
SELECT
`Order ID`,
MAX(`Qty`) AS `Qty`,
MAX(`Option`) AS `Option`,
MAX(`Size`) AS `Size`,
MAX(`Product`) AS `Product`,
CONCAT_WS(' ',
MAX(CONCAT(
CASE WHEN `Qty` > 1 THEN CONCAT(`Qty`, 'x ') ELSE '' END,
`Option`, ' / ', `Product`,
CASE WHEN COALESCE(`Size`, '') != '' THEN CONCAT(' ', `Size`) ELSE '' END
)),
MAX(`Size`)
) AS `Ref`
FROM (
SELECT
o.`order_id` AS `Order ID`,
op.`quantity` AS `Qty`,
CASE
WHEN oo.`value` LIKE '%Red%' THEN 'R'
WHEN oo.`value` LIKE '%Blue%' THEN 'Bl'
END AS `Option`,
CASE
WHEN op.`name` LIKE '%t-shirt%' THEN 'Tee'
WHEN op.`name` LIKE '%hoodie%' THEN 'Hood'
ELSE op.`name`
END AS `Product`,
CASE
WHEN oo.`value` LIKE '%Small%' THEN 'S'
WHEN oo.`value` LIKE '%Medium%' THEN 'M'
WHEN oo.`value` LIKE '%Large%' THEN 'L'
ELSE ''
END AS `Size`
FROM `order` AS o
INNER JOIN `order_option` AS oo ON o.`order_id` = oo.`order_id`
INNER JOIN `order_product` AS op ON op.`order_product_id` = oo.`order_product_id`
) AS `Orders`
GROUP BY `Order ID`
ORDER BY `Order ID` DESC
我对您的代码进行了更改,例如对表使用适当的连接和别名。
见 demo.
结果:
| Order ID | Qty | Option | Size | Product | Ref |
| -------- | --- | ------ | ---- | ------- | -------------- |
| 12346 | 2 | Bl | S | Hood | 2x Bl / Hood S |
| 12345 | 1 | R | L | Tee | R / Tee L |