2017-05-30 3 views
1

so versuche ich, einen kleinen Online-Shop zu machen. Ich habe 2 Tabellen: cart_products und order_products. In diesen Tabellen können zwei Arten von Artikeln in den Warenkorb hinzugefügt werden: promotion und products. Diese beiden Arten von Artikeln werden in verschiedenen Tabellen gespeichert: promotions und products.Fügen Sie Daten von einer Tabelle in eine andere - MySQL

Zunächst werden alle Produkte/Aktionen werden dem cart Tisch, sobald der Benutzer checkt sie auf die orders Tabelle übertragen über und aus der cart Tabelle gelöscht. Wenn das ausgewählte Element ein Produkt ist, wird der Wert promotion_id standardmäßig auf 0 gesetzt. Und umgekehrt, wenn es sich bei dem ausgewählten Artikel um eine Werbeaktion handelt. Das ist mein Grundstruktur:

cart_products

---------------------------------------------------------------------------------- 
| cart_products_id | cart_id | product_id | promotion_id  | quantity | 
---------------------------------------------------------------------------------- 
| 6    |  1  | 5   | 0     | 2 
--------------------------------------------------------------------------------- 

order_products


---------------------------------------------------------------------------------- 
| order_id | user_id | product_id | promotion_id  | price | quantity | 
---------------------------------------------------------------------------------- 

Das Problem, das ich habe, ist zu LEFT JOIN versuchen, die Produkte/promotions die price des ausgewählten zu bekommen Artikel. Das ist meine Abfrage bisher.

INSERT INTO order_details (order_id, user_id, product_id, promotion_id, price, quantity) 
VALUES(
    '6', 
    '7', 
    (SELECT 
    cart_products.product_id, 
    cart_products.promotion_id, 
    IF(cart_products.promotion_id = '0', products.price, promotions.price), 
    cart_products.quantity 

    FROM cart_products 


    LEFT JOIN products 
    ON cart_products.product_id = products.product_id 

    LEFT JOIN promotions 
    cart_products.promotion_id = promotions.promotion_id 

    WHERE cart_products.cart_id = '6') 
) 

Allerdings gibt dies einen Fehler Not unique table/alias. Weiß jemand, wie ich das machen kann? Jede Hilfe wird sehr geschätzt!

Antwort

3

Statt Werte zu definieren, wie Sie haben, können Sie einfach Konstanten wählen, so dass Sie die INSERT INTO SELECT-Syntax verwenden:

INSERT INTO order_details (order_id, user_id, product_id, promotion_id, price, quantity) 
SELECT (
    '6', 
    '7', 
    cart_products.product_id, 
    cart_products.promotion_id, 
    IF(cart_products.promotion_id = '0', products.price, promotions.price), 
    cart_products.quantity 
    FROM cart_products 
    LEFT JOIN products 
    ON cart_products.product_id = products.product_id 
    LEFT JOIN promotions 
    ON cart_products.promotion_id = promotions.promotion_id 
    WHERE cart_products.cart_id = '6' 
) 

Auch ich glaube, dass Sie vergessen haben, eine „ON“ Klausel auf Ihre zweite links join

+1

Danke, ich musste am Ende die 'SELECT' innerhalb der Klammer setzen, damit es funktioniert, sonst gab es mir einen Fehler. Aber ich habe es zur Arbeit gebracht. –

+0

Froh meine Antwort war akzeptabel für Sie @BrianMoreno – AlVaz

Verwandte Themen