dsy19890123 2017-05-30 02:45
浏览 67
已采纳

将数据从一个表插入另一个表 - MySQL

so I'm trying to make a small online store. I have 2 tables: cart_products and order_products. Inside these tables, two types of items that can be added into the cart: promotion and products. These two types of items are stored on different tables called: promotions and products.

Initially all products/promotions are added to the cart table, once the user checks out they are transferred over to the orders table and deleted from the cart table.

If the selected item is a product then the promotion_id value is set to 0 by default. And vice versa if the item selected is a promotion. This is my basic structure:

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 |
----------------------------------------------------------------------------------

The problem I'm having is trying to LEFT JOIN the products/promotions to get the price of the selected item. This is my query so far.

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')
)

However, this gives my an error Not unique table/alias. Does anyone know how I can go about this? Any help is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • douke6881 2017-05-30 02:50
    关注

    Instead of defining values like you have, you can simply select constants so that you can use the INSERT INTO SELECT syntax:

    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'
    )
    

    Also, I believe you forgot an "ON" clause on your second left join

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了