私は小さなオンラインストアを作ろうとしています。私は2つのテーブル:cart_products
とorder_products
を持っています。これらの表の中には、カートに追加できる2種類の品目:promotion
とproducts
があります。これらの2つのタイプのアイテムは、promotions
とproducts
という異なるテーブルに格納されています。あるテーブルから別のテーブルにデータを挿入する -
最初に、ユーザがorders
テーブルに転送され、cart
テーブルから削除されたことをユーザが確認すると、すべての製品/プロモーションがcart
テーブルに追加されます。
選択したアイテムが製品の場合、promotion_id
の値はデフォルトで0
に設定されます。選択したアイテムがプロモーションの場合はその逆です。
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 |
----------------------------------------------------------------------------------
私はLEFT JOIN
に製品をしようとしていることだ問題/プロモーションは、選択のprice
を取得する:これが私の基本的な構造であり、項目。これはこれまでの私の質問です。
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')
)
ただし、これはエラーNot unique table/alias
です。誰も私がこれについてどうやって行くことができるか知っていますか?どんな助けでも大歓迎です!
ありがとうございます。ありがとうございました。私は「SELECT」をかっこの中に入れなければなりませんでした。そうでなければエラーになりました。しかし、私はそれを働かせる。 –
私の答えはあなたに受け入れられて良かった@BrianMoreno – AlVaz