2009-09-26 46 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