GROUP_CONCAT中的MySQL GROUP_CONCAT具有不同的Group By值

时间:2014-06-16 02:08:24

标签: sql

我试图让group_concat在另一个group_concat中工作,但是按不同的值分组。

3表产品,客户和产品_客户(包含每个客户购买的产品和尺寸)


#Creates the Customer Table
CREATE TABLE Customer
(
Cus_Code INT AUTO_INCREMENT PRIMARY KEY,
Cus_Name VARCHAR(20)
);

#Creates the Product Table
CREATE TABLE Product
(
Prod_Code INT AUTO_INCREMENT PRIMARY KEY,
Prod_Name VARCHAR(30)
);

#Creates the Product_Customer Table
CREATE TABLE Product_Customer
(
Cus_Code INT references Customer(Cus_Code),
Prod_Code INT references Product(Prod_Code),
Size INT,
);

样本数据

#Inserts data into Customer Table
INSERT INTO Customer (Cus_Name)
VALUES
('Aaron')
('Bob')
('Charlie')

#Inserts data into Product Table
INSERT INTO Product (Prod_Name)
VALUES
('A')
('B')
('C')

#Inserts data into Product_Customer Table
INSERT INTO Product_Customer (Cus_Code, Prod_Code, Size)
VALUES
(1, 1, 1),
(1, 1, 2),
(1, 2, 1),
(2, 1, 1),
(2, 2, 1),
(2, 2, 2),
(3, 1, 1),
(3, 2, 1),
(3, 3, 1),
(3, 3, 2)

Desired Output Something like this

Customer Name   |   Product(Size)
Aaron           |   A(1,2), B(1)
Bob             |   A(1), B(1,2)
Charlie         |   A(1), B(1), C(1,2)

所以我需要按product_code分组的大小,然后按客户代码

分组

我尝试过以下各种变化,但无济于事

SELECT Customer.Cus_Name, GROUP_CONCAT(DISTINCT Product.Prod_Code, '(', s.list, ')' SEPARATOR ', ') AS 'Products'
    FROM Product
    JOIN (
    SELECT Product.Prod_Code AS id, GROUP_CONCAT(DISTINCT Product_Customer.Size SEPARATOR ',') AS list
    FROM Product
    INNER JOIN Product_Customer ON Product.Prod_Code = Product_Customer.Prod_Code
    GROUP BY id;
    ) AS s ON s.id = Product_Customer.Prod_Code
    INNER JOIN Product_Customer ON Product.Prod_Code = Product_Customer.Prod_Code
    INNER JOIN Customer ON Product_Customer.Cus_Code = Customer.Cus_Code
    GROUP BY Customer.Cus_Code;

它似乎包括为该产品购买的所有尺寸,而不是每个客户购买的尺寸。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

我认为以下版本可以满足您的需求:

SELECT c.Cus_Name, ps.prodsizes
FROM Customer c JOIN
     (select cus_code, group_concat(prod_code, '(', sizes, ')' separator ', ') as prodsizes
      from (select pc.cus_code, pc.prod_code, group_concat(distinct p.size separator ',') as sizes
            from Product_Customer pc join
                 Product p
                 on pc.prod_code = p.prod_code
            group by pc.cus_code, pc.prod_code
           ) cp
      group by cus_code
    ) ps
    on ps.cus_code = c.cus_code
GROUP BY c.Cus_Code;

请注意,有两个级别的聚合可以将产品和尺寸放在一起,首先是在客户产品级别,然后是客户级别。

我还引入了表别名,使查询更容易编写和读取。外部级别不需要distinct,因为重复项在子查询中组合。

答案 1 :(得分:0)

SELECT  c.Cus_Name,
        GROUP_CONCAT(cncat.Size ORDER BY cncat.Prod_Name SEPARATOR ', ') AS Size
FROM    Customer c
        INNER JOIN
        (
            SELECT  pc.Cus_Code, 
                    p.Prod_Name,
                    CONCAT(p.Prod_Name, '(', GROUP_CONCAT(pc.size), ')') Size
            FROM    Product_Customer pc
                    INNER JOIN Product p
                        ON pc.Prod_Code = p.Prod_Code
            GROUP   BY pc.Cus_Code, 
                        p.Prod_Name
        ) AS cncat
            ON  c.Cus_Code = cncat.Cus_Code
GROUP   BY c.Cus_Name
相关问题