在Access中合并两个共享相同值的表

时间:2014-05-24 01:03:37

标签: sql ms-access

我是数据库管理的新手,所以请不要评判我。我有两个XML文件,我将其作为表导入Access。现在我有两张桌子(电影和电影1)。
Movies table has fields: Title and Year.
Movies1 table has fields: Title and Genre.
我想合并这些表来创建一个带有字段的新表:Title, Year, Genre但仅适用于那些具有相同名称的字段。反正有吗?如果答案是“是”,那么如何?

2 个答案:

答案 0 :(得分:1)

使用JOIN从下面的电影和电影1表中获取匹配的记录(发布的示例查询是按照SQL语法而不是特定于MS ACCESS语法)。您必须根据ms access

更正语法
select m.Title, m.Year, m1.Genre
from movies as m inner join
     movies1 as m1
     on m.title = m1.title

mm1table alias,用于限定表格列。示例:m.title表示将从title表中选择movies列,这样可以提高易读性。

修改

尝试不带表别名的查询

select movies.Title, movies.Year, movies1.Genre
from movies inner join
     movies1 
     on movies.title = movies1.title

<强> EDIT1:

如果要将查询结果插入到不同的/新表中,则表单如下所示。有关详细信息,请参阅Here

INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase] 
SELECT [source.]field1[, field2[, …]     FROM tableexpression 

在你的情况下它应该是

INSERT INTO processed_movie
 select m.Title, m.Year, m1.Genre
 from movies as m inner join
     movies1 as m1
     on m.title = m1.title

答案 1 :(得分:0)

创建新表(movies2)并运行:

insert into movies2(title, year) select title, year from movies

update m2
  set m2.genre = m1.genre
  from movies2 as m2
  inner join movies1 as m1 on m2.title = m1.title

insert into movies2(title, genre)
  select title, genre
  from movies1
  where title not in (select title from movies2)