gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:32:42 25 4
gpt4 key购买 nike

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

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

我实际上需要对多个辅助表执行此操作,但只要我能弄清楚如何在一个表上执行此操作,我就可以在我需要的每个表上手动运行查询。

到目前为止,我一直在尝试使用 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

此查询无效:(

主表

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

辅助表

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

查询后的主表

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

提前感谢您的帮助。

最佳答案

我不认为 and not in m.groupid 是有效的 sql。带有子选择的 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
)

关于MYSQL 使用 2 列限定的其他表的值更新一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30092690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com