Insert Data From One Table To Another - MySQL

Brian Moreno

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!

AlVaz

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert data from a table to another one in MySQL with where condition

From Dev

MySQL - Insert table data from another table

From Dev

MySQL - Insert table data from another table

From Dev

Transfer data from one table to another on insert

From Dev

Insert data from one database to another in mysql

From Dev

MySQL insert into column data from another table

From Dev

How to insert and retrive data from one table to another table in mysql database

From Dev

Data from one table to another in MySQL

From Dev

Move data from one MySQL table to another

From Dev

Moving data from one mysql table to another

From Dev

Move data from one MySQL table to another

From Dev

MySQL add data from one table to another

From Dev

Procedure to insert a data from one table to another table after calculations

From Dev

Insert Data From One Table to Another Table with default values

From Dev

Insert Data From One Table to Another Table and Add New Values

From Dev

insert data from one table to a single column another table with no relation

From Dev

MySQL: Is there a way to insert values from one table to another?

From Dev

MySQL SELECT from one table and INSERT in another - Performance

From Dev

Insert ID from one table to another in MySQL php

From Dev

mysql insert unique values from one column to a column of another table

From Dev

MySQL: Is there a way to insert values from one table to another?

From Dev

MSSQL Insert Data From One Table To Another With Fixed Value

From Dev

INSERT data from one table to another where ids match

From Dev

Insert Data from one table to another leaving the already existing rows

From Dev

Procedure to insert data from one column into two columns in another table

From Dev

want to insert data from one table into another in php?

From Dev

insert data from one table to another - Not Working - possible bug?

From Dev

MySQL Insert INTO using SELECT from another table with additional data

From Dev

mysql insert with value, with selected data from another table

Related Related

  1. 1

    Insert data from a table to another one in MySQL with where condition

  2. 2

    MySQL - Insert table data from another table

  3. 3

    MySQL - Insert table data from another table

  4. 4

    Transfer data from one table to another on insert

  5. 5

    Insert data from one database to another in mysql

  6. 6

    MySQL insert into column data from another table

  7. 7

    How to insert and retrive data from one table to another table in mysql database

  8. 8

    Data from one table to another in MySQL

  9. 9

    Move data from one MySQL table to another

  10. 10

    Moving data from one mysql table to another

  11. 11

    Move data from one MySQL table to another

  12. 12

    MySQL add data from one table to another

  13. 13

    Procedure to insert a data from one table to another table after calculations

  14. 14

    Insert Data From One Table to Another Table with default values

  15. 15

    Insert Data From One Table to Another Table and Add New Values

  16. 16

    insert data from one table to a single column another table with no relation

  17. 17

    MySQL: Is there a way to insert values from one table to another?

  18. 18

    MySQL SELECT from one table and INSERT in another - Performance

  19. 19

    Insert ID from one table to another in MySQL php

  20. 20

    mysql insert unique values from one column to a column of another table

  21. 21

    MySQL: Is there a way to insert values from one table to another?

  22. 22

    MSSQL Insert Data From One Table To Another With Fixed Value

  23. 23

    INSERT data from one table to another where ids match

  24. 24

    Insert Data from one table to another leaving the already existing rows

  25. 25

    Procedure to insert data from one column into two columns in another table

  26. 26

    want to insert data from one table into another in php?

  27. 27

    insert data from one table to another - Not Working - possible bug?

  28. 28

    MySQL Insert INTO using SELECT from another table with additional data

  29. 29

    mysql insert with value, with selected data from another table

HotTag

Archive