2017-02-01 9 views
1

Zunächst entschuldigen Sie für den vagen Titel, ich weiß wirklich nicht, wie ich es ausdrücken soll und ich denke, das ist, warum ich nicht in der Lage gewesen bin, eine Lösung bereits zu finden.MySQL-Gruppe Um einzigartige Kombinationen zu finden

Mit einer modifizierten Installation von OSCommerce versuche ich, eindeutige Kombinationen von bestellten Produktoptionen/Attributen in einer Tabelle für einen benutzerdefinierten Bericht zu finden. Jede Option/jedes Attribut eines Produkts wird in einer Zeile gespeichert. Ein Produkt kann (und hat) mehr als eine Option/ein Attribut.

Ich habe die folgende SQL-Abfrage

SELECT 
sum(op.products_quantity) AS attrCnt, 
opa.products_options AS prodOption, 
opa.products_options_values AS prodOptionValue 
FROM 
store_orders_products_attributes opa 
LEFT JOIN store_orders o ON o.orders_id = opa.orders_id 
LEFT JOIN store_orders_products op ON op.orders_products_id = opa.orders_products_id 
WHERE 
o.customers_id = '99999' 
AND (o.date_purchased BETWEEN CAST('2017-01-03' AS DATE) 
AND CAST('2017-02-01' AS DATE)) 
AND op.products_id = 88888 
GROUP BY 
opa.products_options_values 
ORDER BY 
opa.products_options, 
opa.products_options_values 

Welche zurück:

+---------+---------------+-----------------+ 
| attrCnt | prodOption | prodOptionValue | 
+---------+---------------+-----------------+ 
|  6 | Select Colour | Blue   | 
|  1 | Select Colour | Yellow   | 
|  1 | Select Size | L    | 
|  2 | Select Size | M    | 
|  4 | Select Size | S    | 
+---------+---------------+-----------------+ 

die Gesamtprodukte für diesen Zeitraum bestellt war 7, 1 Groß, 2 Medium, 4 Klein, - 6 Blau, 1 Gelb. Von dass ich nicht sagen kann, ob die L (groß) war blau oder gelb usw.

Das Ergebnis Ich mag wäre so etwas wie unten ist (ob es sich um L - Blau oder Blau - L spielt keine Rolle):

+---------+-----------------+ 
| attrCnt | prodOptionCombo | 
+---------+-----------------+ 
|  1 | L - Blue  | 
|  2 | M - Blue  | 
|  3 | S - Blue  | 
|  1 | S - Yellow  | 
+---------+-----------------+ 

Die Tabelle mit diesen Informationen ist store_orders_products_attributes:

products_options Spalte jede Klassifikation Text Gruppierung (freier Text), wie Select Flavor enthalten könnte, wählen xyz etc, so wird es nicht immer Farbe/Größe

Es sind nicht immer zwei Ptionen pro Produkt, es könnte 0, 1 oder 12 oder mehr sein.

+-------------------------------+-----------+--------------------+------------------+-------------------------+ 
| orders_products_attributes_id | orders_id | orders_products_id | products_options | products_options_values | 
+-------------------------------+-----------+--------------------+------------------+-------------------------+ 
|       1420 |  596 |    2626 | Select Colour | Blue     | 
|       1421 |  596 |    2626 | Select Size  | M      | 
|       1438 |  600 |    2656 | Select Colour | Blue     | 
|       1439 |  600 |    2656 | Select Size  | M      | 
|       1445 |  601 |    2668 | Select Colour | Blue     | 
|       1446 |  601 |    2668 | Select Size  | S      | 
|       1447 |  602 |    2671 | Select Colour | Yellow     | 
|       1448 |  602 |    2671 | Select Size  | S      | 
|       1464 |  611 |    2705 | Select Colour | Blue     | 
|       1465 |  611 |    2705 | Select Size  | S      | 
|       1502 |  634 |    2791 | Select Colour | Blue     | 
|       1503 |  634 |    2791 | Select Size  | L      | 
    +-------------------------------+-----------+--------------------+------------------+-------------------------+ 

store_orders_products Tabelle enthält:

+--------------------+-----------+-------------+----------------+---------------+-------------------+ 
| orders_products_id | orders_id | products_id | products_model | products_name | products_quantity | 
+--------------------+-----------+-------------+----------------+---------------+-------------------+ 
|    2626 |  596 |  88888 | Code123  | Gloves  |     1 | 
|    2656 |  600 |  88888 | Code123  | Gloves  |     1 | 
|    2668 |  601 |  88888 | Code123  | Gloves  |     1 | 
|    2671 |  602 |  88888 | Code123  | Gloves  |     1 | 
|    2705 |  611 |  88888 | Code123  | Gloves  |     2 | 
|    2791 |  634 |  88888 | Code123  | Gloves  |     1 | 
+--------------------+-----------+-------------+----------------+---------------+-------------------+ 

Irgendwelche Ideen?

+0

ich nicht heraus kann, warum sind sie zwei verschiedene Attribute (Farbe und Größe) in der gleichen Spalte (products_options_values) zu speichern, ist das nicht ein Konzeptionsproblem? – mounaim

+0

@mounaim Sie müssten mit Harald Ponce de Leon, dem ursprünglichen Schöpfer von OSCommerce, sprechen. Die Informationen werden vor der Bestellung auf ähnliche Weise gespeichert, so dass sie nach der Bestellung in ähnlicher Weise gespeichert werden. Die Optionen können unterschiedliches Preis/Gewicht angewandt haben +/- Geld kg usw. Bestellen Anzeige ist Produktname - Wählen Sie Farbe = Blau - Wählen Sie Größe L = Als Folge der Daten in dieser Art und Weise zu speichern. – PeteB

+0

@mounaim auch die Menge der Optionen ist unbegrenzt, wenn Größe, Farbe, xyz usw. hatten ihre eigenen Spalten dort könnte 100 von Spalten sein – PeteB

Antwort

0

Um damit zu beginnen, benutzen Sie diese Unterabfrage statt der rohen Tabelle:

(
select a1.orders_id, 
     a1.products_options_values as sizeval, 
     a2.products_options_values as colourval 
from store_orders_products_attributes a1 
inner join store_orders_products_attributes a2 
    on a1.orders_id = a2.orders_id 
where a1.products_options = 'Size' 
and a2.products_options = 'Colour' -- Yay, another brit 
) x1 
+0

Großartige Idee, aber und Entschuldigung hätte ich in der ersten post gesagt haben, könnte products_options jede Klassifizierung enthalten die der Kunde eingegeben hat, wählen Sie Flavor, Select Capacity etc – PeteB

Verwandte Themen