Jump to content

SQL Abfrage - Ein Kunde mit mehreren Artikeln


Recommended Posts

Hallo,

ich nutze folgende Datenbankabfrage um mir eine csv Datei erstellen zu lassen. Leider wird wenn ein Kunde mehrere Artikel kauft nur der erste in der Datei abgebildet. Was muss ich Einpflegen um alle Artikel zu sehen?

SELECT d.id_order, os.name AS Status, d.product_name AS Produkt, ROUND(d.product_price,2) AS Preis, d.product_quantity AS Menge, o.payment AS Bezahlung, o.date_upd AS Datum, CONCAT_WS(' ', a.firstname, a.lastname) AS Kunde, a.address1 AS Strasse, a.postcode AS PLZ, a.city AS Ort, cl.name AS Land, gl.name AS Kundengruppe
    FROM ps_order_detail d
    LEFT JOIN ps_orders o ON (d.id_order = o.id_order)
    LEFT JOIN ps_customer g ON (o.id_customer = g.id_customer)
    LEFT JOIN ps_address AS a ON (o.id_customer = a.id_customer)
    LEFT JOIN ps_country_lang AS cl ON (a.id_country = cl.id_country )
    LEFT JOIN ps_group_lang gl ON (g.id_default_group = gl.id_group)
    LEFT JOIN ps_order_state_lang os ON (o.current_state = os.id_order_state)

 GROUP BY d.id_order DESC

 

Link to comment
Share on other sites

Hallo,
Sie können für den Produktnamen diesen verwenden, der die Produkte von verkettet:
 
,(select group_concat(ps_order_detail.product_name, ' | ') from ps_order_detail where ps_order_detail.id_order = o.id_order ) AS Produkt

Ich hoffe, dass ich helfen konnte.
Schönen Tag noch, Leo.

Link to comment
Share on other sites

Hi, danke für die Antwort! Sorry bin totaler Anfänger

So funktioniert es nicht. Was mache ich falsch?

SELECT d.id_order, os.name AS Status, d.product_name AS Produkt, ROUND(d.product_price,2) AS Preis, d.product_quantity AS Menge, o.payment AS Bezahlung, o.date_upd AS Datum, CONCAT_WS(' ', a.firstname, a.lastname) AS Kunde, a.address1 AS Strasse, a.postcode AS PLZ, a.city AS Ort, cl.name AS Land, gl.name AS Kundengruppe, (select group_concat(ps_order_detail.product_name, ' | ') from ps_order_detail where ps_order_detail.id_order = o.id_order ) AS Produkt
    FROM ps_order_detail d
    LEFT JOIN ps_orders o ON (d.id_order = o.id_order)
    LEFT JOIN ps_customer g ON (o.id_customer = g.id_customer)
    LEFT JOIN ps_address AS a ON (o.id_customer = a.id_customer)
    LEFT JOIN ps_country_lang AS cl ON (a.id_country = cl.id_country )
    LEFT JOIN ps_group_lang gl ON (g.id_default_group = gl.id_group)
    LEFT JOIN ps_order_state_lang os ON (o.current_state = os.id_order_state)

 GROUP BY d.id_order DESC

 

Link to comment
Share on other sites

Hallo ianmoone,

deine Abfrage im ersten post ist soweit schon ok, aber durch die letzte Anweisung

GROUP BY d.id_order DESC

fasst du ja deine eigentlich aus mehreren Zeilen bestehende Bestellung auf nur eine zusammen, die anderen werden dadurch unterschlagen.

Wenn du diese Zeile einfach mal weglässt wirst du sehen, daß nun alle Produkte der Bestellungen angezeigt werden, allerdings werden dann auch Einzelbestellungen mehrfach dargestellt. Du mußt nun also eine GROUP-Anweisung finden, die die Doppelnennungen ausblendet. Das könnte zum Beispiel so aussehen:

SELECT d.id_order, os.name AS Status, d.product_name AS Produkt, ROUND(d.product_price,2) AS Preis, d.product_quantity AS Menge, o.payment AS Bezahlung, o.date_upd AS Datum, CONCAT_WS(' ', a.firstname, a.lastname) AS Kunde, a.address1 AS Strasse, a.postcode AS PLZ, a.city AS Ort, cl.name AS Land, gl.name AS Kundengruppe
    FROM ps_order_detail d
    LEFT JOIN ps_orders o ON (d.id_order = o.id_order)
    LEFT JOIN ps_customer g ON (o.id_customer = g.id_customer)
    LEFT JOIN ps_address AS a ON (o.id_customer = a.id_customer)
    LEFT JOIN ps_country_lang AS cl ON (a.id_country = cl.id_country )
    LEFT JOIN ps_group_lang gl ON (g.id_default_group = gl.id_group)
    LEFT JOIN ps_order_state_lang os ON (o.current_state = os.id_order_state)
GROUP by  CONCAT_WS(' ', d.product_name, g.lastname, g.firstname,d.id_order ) 
ORDER BY d.id_order DESC

Grüsse
Whiley

Link to comment
Share on other sites

Hallo ianmoone,

das sind  Basics von sql, da wird hier kaum jemand Lust haben dir das zu erklären. Im Netz gibt es genügend Anfänger-Tutorials.

Aber wenn du die Spalte schon gefunden hast, warum ergänzt du deine select Abfrage dann nicht mit o.id_adress_delivery

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...