如果另一个表中不存在条目,则将记录插入表中 - 并进行额外的扭曲

bon*_*o46 17 sql ms-access insert not-exists

嗨,你们所有强大的SQLsuperheros在那里..任何人都可以救我从迫在眉睫的灾难和毁灭?

我正在使用Microsoft Access SQL.我想在一个表(table1)中选择不出现在另一个表(table2)中的记录,然后在table2中插入基于table1中记录的新记录,如下所示:

[table1] file_index:filename

[table2] file_index:celeb_name

我想要:

从table1中选择所有记录,其中[filename]与aud一样 ,其对应的[file_index]值在table2中不存在,带有[celeb_name] ='Audrey Hepburn'字段

有了这个选择,我想在[table2]中插入一条新记录

[file_index] = [table1].[file_index] [celeb_name] ='奥黛丽赫本'

[table1]中的[file_index]和[table2] [table1]中的一条记录与[table2]中的许多记录之间存在一对多的关系.

非常感谢

Tor*_*amo 16

这会吗?显然添加一些方括号和东西.不要自己进入Access.

INSERT INTO table2 (file_index, celeb_name)
SELECT file_index, 'Audrey Hepburn'
FROM table1
WHERE filename = 'aud'
  AND file_index NOT IN (SELECT DISTINCT file_index 
                         FROM table2 
                         WHERE celeb_name = 'Audrey Hepburn')
Run Code Online (Sandbox Code Playgroud)

  • @ David-W-Fenton - 任何使用Access并期望性能的人应该真正寻找其他地方......;) (5认同)
  • @ David-W-Fenton - 我的意思是,您使用Access来访问桌面应用程序,而不是使用具有PB级数据的服务器群集.因此,由此子查询引起的任何4 ms延迟都不会也永远不会成为问题.另请注意,Access具有2 GB的数据库大小限制,因此我没有看到很多情况需要一段时间. (4认同)