2011-06-02 76 views
1

我有2個表,臨時和永久表。 我的目標是將臨時數據複製到永久表中,但必須更新permanant表。複製表上的MYSQL複製密鑰更新

下面是表

### Temp. table 
CREATE TABLE `tb_temp_data` (
    `key_id` varchar(20) NOT NULL DEFAULT '', 
    `h00` int(11) DEFAULT '0', 
    `h01` int(11) DEFAULT '0', 
    `h02` int(11) DEFAULT '0', 
    `h03` int(11) DEFAULT '0', 
    `h04` int(11) DEFAULT '0', 
    `h05` int(11) DEFAULT '0', 
    `h06` int(11) DEFAULT '0', 
    `h07` int(11) DEFAULT '0', 
    `h08` int(11) DEFAULT '0', 
    `h09` int(11) DEFAULT '0', 
    `h10` int(11) DEFAULT '0', 
    `h11` int(11) DEFAULT '0', 
    `h12` int(11) DEFAULT '0', 
    `h13` int(11) DEFAULT '0', 
    `h14` int(11) DEFAULT '0', 
    `h15` int(11) DEFAULT '0', 
    `h16` int(11) DEFAULT '0', 
    `h17` int(11) DEFAULT '0', 
    `h18` int(11) DEFAULT '0', 
    `h19` int(11) DEFAULT '0', 
    `h20` int(11) DEFAULT '0', 
    `h21` int(11) DEFAULT '0', 
    `h22` int(11) DEFAULT '0', 
    `h23` int(11) DEFAULT '0', 
    `grand_total` int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`key_id`) 
) ENGINE=MyISAM; 

### Permanant table 
CREATE TABLE `tb_permanant_data` (
    `key_id` varchar(20) NOT NULL DEFAULT '', 
    `h00` int(11) DEFAULT '0', 
    `h01` int(11) DEFAULT '0', 
    `h02` int(11) DEFAULT '0', 
    `h03` int(11) DEFAULT '0', 
    `h04` int(11) DEFAULT '0', 
    `h05` int(11) DEFAULT '0', 
    `h06` int(11) DEFAULT '0', 
    `h07` int(11) DEFAULT '0', 
    `h08` int(11) DEFAULT '0', 
    `h09` int(11) DEFAULT '0', 
    `h10` int(11) DEFAULT '0', 
    `h11` int(11) DEFAULT '0', 
    `h12` int(11) DEFAULT '0', 
    `h13` int(11) DEFAULT '0', 
    `h14` int(11) DEFAULT '0', 
    `h15` int(11) DEFAULT '0', 
    `h16` int(11) DEFAULT '0', 
    `h17` int(11) DEFAULT '0', 
    `h18` int(11) DEFAULT '0', 
    `h19` int(11) DEFAULT '0', 
    `h20` int(11) DEFAULT '0', 
    `h21` int(11) DEFAULT '0', 
    `h22` int(11) DEFAULT '0', 
    `h23` int(11) DEFAULT '0', 
    `grand_total` int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`key_id`) 
) ENGINE=MyISAM; 

INSERT INTO tb_permanant_data 
     SELECT * FROM tb_temp_data ; 
     ON DUPLICATE KEY UPDATE h00 = ? 

我要確保所有H00 H23之前將通過增加價值爲tb_permanant_data被更新,但我對如何做到這一點不知道......

誰能幫忙?

感謝

+1

這24個'hxx'字段是否存儲不同的數據?或者只是一天中的每個小時的數據?如果是第二種情況,則表格需要標準化。 – 2011-06-02 10:13:08

+0

是的,可以存儲24小時。必須做這個設計,因爲鍵太多了......那麼sql語句怎麼樣? – 2011-06-02 10:33:03

回答

2
INSERT INTO tb_permanant_data SELECT * FROM tb_temp_data 
ON DUPLICATE KEY UPDATE 
    h00 = VALUES(h00), 
    h01 = VALUES(h01), 
    h02 = VALUES(h02), 
    h03 = VALUES(h03), 
    h04 = VALUES(h04), 
    h05 = VALUES(h05), 
    h06 = VALUES(h06), 
    h07 = VALUES(h07), 
    h08 = VALUES(h08), 
    h09 = VALUES(h09), 
    h10 = VALUES(h10), 
    h11 = VALUES(h11), 
    h12 = VALUES(h12), 
    h13 = VALUES(h13), 
    h14 = VALUES(h14), 
    h15 = VALUES(h15), 
    h16 = VALUES(h16), 
    h17 = VALUES(h17), 
    h18 = VALUES(h18), 
    h19 = VALUES(h19), 
    h20 = VALUES(h20), 
    h21 = VALUES(h21), 
    h22 = VALUES(h22), 
    h23 = VALUES(h23); 

注意,表tb_permanant_data可以包含不存在tb_temp_data(由id)行。