从表视图行更新 MYSQL 中的行

发布于 2025-01-04 05:30:37 字数 285 浏览 1 评论 0原文

我很困惑。

假设我有一个表“1”,包含三列“A”、“B”、“C”。列“C”有一些 NULL 值。另一个表“2”具有列“A”(与表“1”匹配)和“C”,其中“C”是完整的。

如何将 MYSQL 中表“2”的值合并到表“1”中?

我已经尝试过,并且发誓它应该有效:

UPDATE 1  
SET 1.C = 2.C  
FROM 1 JOIN 2  
ON 1.A = 2.A  
WHERE 1.C IS NULL;

还有线索吗?提示?想法?

I'm stumped.

Let's say I've got a table, '1', with three columns, 'A', 'B', 'C'. Column 'C' has some NULL values. Another table, '2', has columns 'A' (that matches table '1') and 'C', where 'C' is complete.

How can I merge the values from table '2' into table '1' in MYSQL?

I've tried, and swore up and down it should work:

UPDATE 1  
SET 1.C = 2.C  
FROM 1 JOIN 2  
ON 1.A = 2.A  
WHERE 1.C IS NULL;

And clues? hints? ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2025-01-11 05:30:37

这有效:

update t1, t2
set t1.c = t2.c
where t1.a = t2.a and t1.c is null;

答案根据更改的要求进行了更新。 T2 可能是一个视图,因此联接可能是一个更好的主意。不过,我对视图了解不多,所以我刚刚移出 UPDATE 行 t2...但我不确定它是否会改变任何内容。

update t1
join t2 on t1.a = t2.a
set t1.c = t2.c
where t1.c is null;

This works:

update t1, t2
set t1.c = t2.c
where t1.a = t2.a and t1.c is null;

Answer updated based on requirements changed. T2 could be a view, so a join might be a better idea. I don't know much about views, though, so I just moved out of the UPDATE line t2... but I'm not sure if it changes anything.

update t1
join t2 on t1.a = t2.a
set t1.c = t2.c
where t1.c is null;
2025-01-11 05:30:37

您的查询是正确的,但尝试使用别名

UPDATE  `tableNameA` `a`  
SET     `a`.`C` = `b`.`C`  
FROM    `a` INNER JOIN `tableNameB` `b` ON `a`.`A` = `b`.`A`  
WHERE   `a`.`C` IS NULL;

我添加了反引号,以防您的某些字段包含保留字

your query is correct but try using Alias:

UPDATE  `tableNameA` `a`  
SET     `a`.`C` = `b`.`C`  
FROM    `a` INNER JOIN `tableNameB` `b` ON `a`.`A` = `b`.`A`  
WHERE   `a`.`C` IS NULL;

I added backtick in case some of your fields contains RESERVED WORD

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文