繁体   English   中英

从一个表到另一个具有相同ID号码的值插入

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

我在同一数据库中有两个表。 我想将一些信息从一个表传递到另一个表。

在第一个表“ products”中,我有products_id,products_length,products_width和products_height,我想将所有信息从该表传递给第二个“ product”。

第二个表“产品”具有以下列product_id,长度,宽度和高度。

在两个表中,产品都是相同的,并且具有相同的ID号。

我只需要有关如何将值从“产品”插入“产品”的帮助。

尝试这个:

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

您应该真正考虑改善现有方案。 您有很多重复的信息,这会导致性能和维护困难。 表产品不应存储给定产品的信息,而应仅在第二个表产品中具有产品的外键。

尝试:

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

如果要插入产品的所有记录,则可以删除WHERE子句。

您只是要这个吗?

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

如果您只是想迁移数据,可以复制表吗? 还是重命名它和列?

首先,我建议您改善数据库架构-复制一些信息。

但要回答,您可以使用:

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

另一个选择是复制表,重命名表,然后重命名列?

请参考文档: http : //dev.mysql.com/doc/refman/5.0/en/insert-select.html

和使用:

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

复制数据之前,还要验证两个表的架构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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