2016-07-16 6 views
0

Ich versuche, von allen Größen zu bekommen, und Ergebnis als eins innerhalb GROUP_CONCAT().Wie bekomme ich SUM() in GROUP_CONCAT()

  • Wenn es gleiches Ergebnis innerhalb GROUP_CONCAT() ist dann können wir SUM()
  • Wenn NULL oder andere Größe product_size.Size_Name verwenden Sie dann nicht bekommen, ein SUM() davon.

Durch die Art und Weise dieses ist meine Frage:

  SELECT 
       product_table.Product_Name, 
       SUM(product_quantity.Quantity) AS 'QUANTITY', 
       GROUP_CONCAT(
       product_quantity.Quantity, 
       ' box/es ', 
       product_size.Size_Name, 
       ' size' SEPARATOR '\n' 
      ) AS 'SIZE' 
      FROM 
       product_quantity 
      RIGHT JOIN 
       product_table 
      ON 
       product_quantity.Product_ID = product_table.Product_ID 
      LEFT JOIN 
       product_size 
      ON 
       product_quantity.Size_ID = product_size.size_ID 
      GROUP BY 
       product_table.Product_Name ASC 

und so ist dies das Ergebnis:

PRODUCT     QUANTITY      SIZE 

Hawaiian Pizza     11     2 - box/es Large size 
                3 - box/es Large size 
                1 - box/es Small size 
                5 - box/es Large size 

Und es ist die richtige Antwort auf das, was ich jetzt zu bekommen versuchen:

PRODUCT     QUANTITY      SIZE 

Hawaiian Pizza     11     10 - box/es Large size 
                1 - box/es Small size 

Aber wie? Irgendeine Idee ??? dieser

Durch die Art und Weise ist meine Tabellenstruktur:

Denn

Tabelle Produkt:

 CREATE TABLE `product_table` (
      `Product_ID` int(40) NOT NULL, 
      `Product_Name` varchar(400) NOT NULL 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


     INSERT INTO `product_table` (`Product_ID`, `Product_Name`) VALUES 
     (1001, 'Hawaiian Pizza'), 
     (1002, 'Chicken Alfredo'), 
     (1003, 'Chicken Quesadillas'), 
     (1004, 'Mexican Pizza'), 
     (1006, 'Beef Pepperoni Pizza'); 

     ALTER TABLE `product_table` 
     ADD PRIMARY KEY (`Product_ID`), 
     ADD KEY `Product_ID` (`Product_ID`); 

     ALTER TABLE `product_table` 
     MODIFY `Product_ID` int(40) NOT NULL AUTO_INCREMENT,  
     AUTO_INCREMENT=1007; 

Tabelle product_size:

 CREATE TABLE `product_size` (
      `Size_ID` int(100) NOT NULL, 
      `Size_Name` varchar(50) NOT NULL 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

     INSERT INTO `product_size` (`Size_ID`, `Size_Name`) VALUES 
     (1, 'Small'), 
     (2, 'Medium'), 
     (3, 'Extra Large'), 
     (4, 'Large'); 

     ALTER TABLE `product_size` 
     ADD PRIMARY KEY (`Size_ID`); 

     ALTER TABLE `product_size` 
     MODIFY `Size_ID` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9; 

Tabelle product_quantity:

 CREATE TABLE `product_quantity` (
      `Quantity_ID` int(100) NOT NULL, 
      `Quantity` int(100) NOT NULL, 
      `Product_ID` int(100) NOT NULL, 
      `Size_ID` int(100) NOT NULL 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

     INSERT INTO `product_quantity` (`Quantity_ID`, `Quantity`, 
     `Product_ID`, `Size_ID`) VALUES 
     (18, 2, 1002, 3), 
     (19, 1, 1001, 2), 
     (20, 2, 1003, 1), 
     (21, 3, 1002, 2), 
     (22, 1, 1002, 3), 
     (23, 2, 1003, 3), 
     (24, 3, 1004, 4), 
     (25, 1, 1003, 3), 
     (26, 3, 1006, 3), 
     (27, 4, 1002, 3), 
     (28, 2, 1004, 4), 
     (29, 4, 1002, 3), 
     (30, 2, 1002, 3), 
     (31, 2, 1002, 2), 
     (32, 3, 1003, 3), 
     (33, 23, 1002, 4); 

     ALTER TABLE `product_quantity` 
     ADD PRIMARY KEY (`Quantity_ID`), 
     ADD KEY `Product_ID` (`Product_ID`), 
     ADD KEY `Size_ID` (`Size_ID`); 

     ALTER TABLE `product_quantity` 
     MODIFY `Quantity_ID` int(100) NOT NULL AUTO_INCREMENT, 
     AUTO_INCREMENT=34; 

      ALTER TABLE `product_quantity` 
      ADD CONSTRAINT `product_quantity_ibfk_1` FOREIGN KEY  
      (`Product_ID`) REFERENCES `product_table` (`Product_ID`) 
      ON DELETE CASCADE ON UPDATE CASCADE, 
      ADD CONSTRAINT `product_quantity_ibfk_2` FOREIGN KEY (`Size_ID`) 
      REFERENCES `product_size` (`Size_ID`) ON DELETE CASCADE ON 
      UPDATE CASCADE; 
+0

Können Sie uns das Datenbeispiel zeigen, das Sie verwenden, um dieses Ergebnis zu generieren? Und Ihre Abfrage stimmt nicht mit diesem Ergebnis überein. Bitte lesen Sie [** How-to-Ask **] (http://stackoverflow.com/help/how-to-ask) \t \t Und hier ist ein großartiger Ort, um [** START **] (http : //spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/) um zu erfahren, wie Sie die Qualität Ihrer Fragen verbessern und bessere Antworten erhalten. –

+0

Nebenbei ist die zweite Aussage hier redundant: 'PRIMARY KEY (Product_ID), HINZUFÜGEN SCHLÜSSEL Product_ID (Product_ID);' – Strawberry

+0

Und beende die Frage – Strawberry

Antwort

0

Ich glaube nicht, Sie brauchen group_concat. eine normale group by sollte es tun.

SELECT product_table.product_name, 
     Sum(product_quantity.quantity), 
     CONCAT(' box/es ', 
       product_size.size_name, 
       ' size') AS 'SIZE' 
FROM product_quantity 
RIGHT JOIN product_table 
     ON product_quantity.product_id = product_table.product_id 
LEFT JOIN product_size 
     ON product_quantity.size_id = product_size.size_id 
GROUP BY product_table.product_name ASC, 
      product_size.size_name 

Liefern Quelldaten und eine genauere Datenausgabe und ich kann eine Beispieldemo erstellen.

+0

Ja, Ihre Antwort ist fast richtig, aber was ich tun muss, ist eine 'DISTINCT' von einem Product_Name und' SUM() 'von allen Quantity. Mit einer einzigen und es gibt keine wiederholte Erwähnung einer 'Size_Name' auf' SIZE' Spalte. – Rhamnold

+0

Ihre Frage ist unvollständig, sagen Sie 'Ergebnis Abfrage wie folgt ', aber nicht Ergebnis –