【问题标题】:merging two tables w/ same structure合并两个具有相同结构的表
【发布时间】:2009-09-26 18:15:04
【问题描述】:

试图合并两个表,但我无法真正理解它。两个表的结构完全相同。但是,我研究过的每个查询似乎都将一个表写在另一个表上。我猜这与两个表共享完全相同的 id 的事实有关。

由于它们共享唯一 ID,我希望为从表 #1 插入表 #2 的数据分配一个新 ID。

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

【问题讨论】:

    标签: sql mysql


    【解决方案1】:

    此查询会将 siteScoring1 和 siteScoring2 的所有唯一记录插入到 siteScoring 表中(不包括它们的 id 列,这将在插入时自动分配):

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多