2009-09-26 49 views
0

試圖合併兩個表,但我無法真正理解它。兩個表格的結構完全相同。但是,我研究過的每個查詢似乎都在另一個表上寫了一個表。我猜這與兩個表共享完全相同的ID的事實有關。將兩個表合併爲相同結構

因爲他們共享獨特的ID我想有一個新的ID分配給插入表##2#表#2中的數據。

CREATE TABLE `siteScoring` (
    `id` mediumint(9) NOT NULL auto_increment, 
    `mid` mediumint(9) NOT NULL, 
    `itemId` varchar(25) NOT NULL, 
    `title` text NOT NULL, 
    `topic` varchar(255) NOT NULL, 
    `url` text NOT NULL, 
    `votes` mediumint(10) NOT NULL, 
    `comments` mediumint(6) NOT NULL, 
    `user` varchar(25) NOT NULL, 
    `itemTime` bigint(25) NOT NULL, 
    `time` bigint(25) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=7930 DEFAULT CHARSET=utf8 

回答

3

這個查詢將插入siteScoring表siteScoring1和siteScoring2的所有獨特的記錄(不包括其ID列,這將是automitcally分配上插入):

INSERT INTO siteScoring(mid, itemid, title, topic, url, 
         votes, comments, user, itemTime, time) 
SELECT mid, itemid, title, topic, url, 
     votes, comments, user, itemTime, time 
FROM siteScoring1 

UNION 

SELECT mid, itemid, title, topic, url, 
     votes, comments, user, itemTime, time 
FROM siteScoring2