简体   繁体   中英

INSERT values from one table to another with the same ID NUMBERS

I have two tables in the same database. I would like to pass some information from one table to the other.

In the first table "products", I have products_id, products_length, products_width and products_height and I would like to pass all the information from that table to a second one called "product".

The second table "product" has the following columns product_id, lenght, width and height.

In both tables the products are the same ones and they have the same ID NUMBER.

I just need help on how to INSERT the values from "products" to "product".

Try this:

CREATE TABLE product LIKE products;
INSERT INTO product SELECT * FROM products;

You should really think of improving the scheme you have. You have a lot of duplicated information which will cause performance and maintenance hell. The table products should not store information for a given product but rather should only have a foreign key to a product in the second table product.

Try:

INSERT INTO product VALUES (product_id, lenght, width, height)
  SELECT products_id, products_lenght, products_width, products_height 
  FROM products
  WHERE products_id=3

You can remove WHERE clause if you want to insert all records of products.

Are you just asking for this?

INSERT INTO
    product (product_id, length, width, height)
SELECT
    products_id
    ,products_length
    ,products_width
    ,products_height
FROM
    products

If you're just trying to migrate data, can you just copy the table? Or rename it and the columns?

Firstly, i'd suggest you improve your DB schema - your duplicating some information.

But to answer you could use:

INSERT INTO product (product_id, length, width, height) 
SELECT products_id ,products_length ,products_width ,products_height FROM products

Another option would be to duplicate the table, rename it and then rename your columns?

Refer the documentation: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

and Use :

INSERT INTO product (product_id, length, width, height) 
SELECT products_id ,products_length ,products_width ,products_height 
FROM products 

Before copying the data, also verify the schema for both tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM