MYSQL 使用 2 列限定的其他 table 值更新一个 table

MYSQL Update one table with the values other table qualified using 2 columns

我一直在努力让这个查询起作用,希望有人能给我一些建议。

我有两个 table。主人和辅助。我需要根据 groupid 和 section id 使用 aux 中而不是 master 中的任何行更新 master。

我实际上需要使用多个辅助 table 来执行此操作,但只要我能弄清楚如何在一个上执行此操作,我就可以手动 运行 每个 table我需要

到目前为止,我一直在尝试使用 select 从 aux 获取应该插入到 master 中的数据。一旦我有了它,我就可以修改它来创建插入查询。 (我希望)

select 
   a.id 
from 
   master as m, 
   aux as a
where 
   a.gid = m.groupid 
and 
   a.sid = m.sectionid 
and 
   not in m.groupid 
and 
   not in m.sectionid

此查询无效:(

大师table

id    groupid    sectionid
1     A Group    21
2     A Group    23
3     B Group    55
4     C Group    999
5     D Group    52A
6     D Group    53

aux table

id    gid        sid
1     A Group    21
2     A Group    22
3     A Group    23
4     B Group    55
5     B Group    55A
6     C Group    999
7     D Group    52A
8     D Group    53
9     D Group    56

master table查询后

id    groupid    sectionid
1     A Group    21
2     A Group    23
3     B Group    55
4     C Group    999
5     D Group    52A
6     D Group    53
7     A Group    22
8     B Group    55A
9     D Group    56

提前感谢您的帮助。

你能试试这个吗...

select 
    id 
from 
    aux as a
where 
    not exists (
        select 
            id 
        from 
            master 
        where 
            groupid = a.gid 
        and 
            sectionid = a.sectionid
    )

SQLFiddle

我认为 and not in m.groupid 无效 sql。 带有子 select 的 not exist 应该可以解决问题:

insert into master (groupid, sectionid)
select a.gid, a.sid
from aux as a
where not exists(
  select *
  from master as m
  where m.groupid = a.gid
  and a.sid = m.sectionid
)