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