2016-05-16 60 views
0

我對如何複製表格沒有任何意見。但這是我的問題,我有table_1有5列,並有數據,然後我有table_2有10列,沒有數據。 table_1中的所有列與table_2中的相同,這意味着table_2中只有5列與table_1中的相同,所以我想將table_2中的其他5列複製到table_1,但不會影響table_1上的數據。我怎樣才能做到這一點?預先感謝。在MySQL中複製表格

TABLE_1 table_1

TABLE_2 enter image description here

預期的輸出上TABLE_1 expected output on table_1

+0

你能告訴我們'table_1'和'table_2'的樣本數據,以便清楚嗎?兩張桌子有什麼關係? –

+0

這是[mysql :: insert into table,data from another table](http://stackoverflow.com/questions/4241621/mysql-insert-into-table-data-from-another-table)你在找什麼? – Spiderman

+0

@renmark,no。我想我需要從table_2複製一些列到table_1 –

回答

1

這裏是你如何能夠複製/複製在表和它的所有信息。

  1. 選擇表
  2. 單擊 「操作」 選項卡
  3. 轉到「複製表(database.table)上:
  4. 選擇並填寫相應信息
+0

我得到了這個錯誤。 「表格table_1已經存在」。我想將table_2的列複製到table_1,我的意思是將col_5複製到col_10。 –

1

您可以使用插入ON DUPLICATE KEY UPDATE來更新現有的行,但是這是基於唯一的關鍵約束,這意味着您必須創建約束或更新現有的約束

它確實需要5列中存在唯一的數據。 因此,當來自table_2的行被插入並且違反唯一約束時,它將更新行而不是完成插入。

ALTER TABLE table_1 
    ADD UNIQUE INDEX ix_unique (col_1, col_2, col_3, col_4, col_5); 

INSERT INTO table_1(col_1, col_2, col_3, col_4, col_5, 
        col_6, col_7, col_8, col_9, col_10) 
SELECT t.col_1, t.col_2, t.col_3, t.col_4, t.col_5, 
     t.col_6, t.col_7, t.col_8, t.col_9, t.col_10 
    FROM table_2 t 
ON DUPLICATE KEY UPDATE col_6 = t.col_6, col_7 = t.col_7, col_8 = t.col_8, col_9 = t.col_9, col_10 = t.col_10; 

INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE

編輯

我能夠重現這在MySQL 5.7和5.5,因爲我已經看到了這個功能。

這是我跑的腳本。

CREATE TABLE IF NOT EXISTS `table_1` (
    `col_1` int(10) unsigned NULL, 
    `col_2` int(10) unsigned NULL, 
    `col_3` int(10) unsigned NULL, 
    `col_4` int(10) unsigned NULL, 
    `col_5` int(10) unsigned NULL, 
    `col_6` int(10) unsigned NULL, 
    `col_7` int(10) unsigned NULL, 
    `col_8` int(10) unsigned NULL, 
    `col_9` int(10) unsigned NULL, 
    `col_10` int(10) unsigned NULL, 
    UNIQUE INDEX ix_unique (col_1, col_2, col_3, col_4, col_5) 
); 


CREATE TABLE IF NOT EXISTS `table_2` (
    `col_1` int(10) unsigned NULL, 
    `col_2` int(10) unsigned NULL, 
    `col_3` int(10) unsigned NULL, 
    `col_4` int(10) unsigned NULL, 
    `col_5` int(10) unsigned NULL, 
    `col_6` int(10) unsigned NULL, 
    `col_7` int(10) unsigned NULL, 
    `col_8` int(10) unsigned NULL, 
    `col_9` int(10) unsigned NULL, 
    `col_10` int(10) unsigned NULL 
); 


INSERT INTO table_1 (`col_1`, `col_2`, `col_3`, `col_4`, `col_5`) 
VALUES 
    (1, 1, 1, 1, 1), 
    (2, 2, 2, 2, 2), 
    (3, 3, 3, 3, 3), 
    (4, 4, 4, 4, 4), 
    (5, 5, 5, 5, 5), 
    (6, 6, 6, 6, 6), 
    (7, 7, 7, 7, 7), 
    (8, 8, 8, 8, 8), 
    (9, 9, 9, 9, 9), 
    (10, 10, 10, 10, 10); 


INSERT INTO table_2 (`col_1`, `col_2`, `col_3`, `col_4`, `col_5`, `col_6`, `col_7`, `col_8`, `col_9`, `col_10`) 
VALUES 
    (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    (2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 
    (3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
    (4, 4, 4, 4, 4, 4, 4, 4, 4, 4), 
    (5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 
    (6, 6, 6, 6, 6, 6, 6, 6, 6, 6), 
    (7, 7, 7, 7, 7, 7, 7, 7, 7, 7), 
    (8, 8, 8, 8, 8, 8, 8, 8, 8, 8), 
    (9, 9, 9, 9, 9, 9, 9, 9, 9, 9), 
    (10, 10, 10, 10, 10, 10, 10, 10, 10, 10); 

INSERT INTO table_1(col_1, col_2, col_3, col_4, col_5, 
        col_6, col_7, col_8, col_9, col_10) 
SELECT t.col_1, t.col_2, t.col_3, t.col_4, t.col_5, 
     t.col_6, t.col_7, t.col_8, t.col_9, t.col_10 
    FROM table_2 t 
ON DUPLICATE KEY UPDATE col_6 = t.col_6, col_7 = t.col_7, col_8 = t.col_8, col_9 = t.col_9, col_10 = t.col_10; 
+0

當我嘗試你的代碼時,這是輸出:無法識別的關鍵字。 (在位置236的「ON」附近) 無法識別的關鍵字。 (在位置239處的「重複」附近) 無法識別的關鍵字。 (在位置249的「KEY」附近) 以前發現了別名。 (在位置268處的「t」附近) 以前發現了別名。 (在位置270處的「col_6」附近) 以前發現了別名。 (位置285附近的「t」) 以前發現了別名。 (在位置287的「col_7」附近) 以前發現別名。 (在位置302附近的「t」)..... –

+0

@DunhillDimaapi:我已經添加了repro步驟以查看是否可以幫助澄清。不知道你正在運行的是哪個版本的mysql,但這是mysql特定的功能,並且問題用mysql標記。 –

+0

呀。也許我的MySQL沒有更新。但無論如何,謝謝。 –