MYSQL:使用另一个相关表中的值更新表
我有两个 MySQL 表,它们的结构如下:
table foo(
foo_id varchar(32),
field1 varchar(32),
field2 varchar(32),
bar_id varchar(32)
);
table bar(
bar_id varchar(32),
field1 varchar(32),
field2 varchar(32)
);
我想更新 foo
表,使 field1
和 field2
具有相同的值作为 bar
表。我已经尝试了以下两个查询,它们都运行没有错误,但没有得到我想要的结果:
UPDATE foo LEFT JOIN bar ON foo.bar_id = bar.bar_id
SET foo.field1 = bar.field1 AND foo.field2 = bar.field2;
也
UPDATE foo,bar SET foo.field1 = bar.field1
AND foo.field2 = bar.field2 WHERE foo.bar_id = bar.bar_id
但都不起作用。我在这里缺少什么?
I have two MySQL tables, here's how they are structured:
table foo(
foo_id varchar(32),
field1 varchar(32),
field2 varchar(32),
bar_id varchar(32)
);
table bar(
bar_id varchar(32),
field1 varchar(32),
field2 varchar(32)
);
I would like to update the foo
table to have the same values for field1
and field2
as the bar
table. I've tried the following two queries, both of which run without error, but don't get me the results I want:
UPDATE foo LEFT JOIN bar ON foo.bar_id = bar.bar_id
SET foo.field1 = bar.field1 AND foo.field2 = bar.field2;
also
UPDATE foo,bar SET foo.field1 = bar.field1
AND foo.field2 = bar.field2 WHERE foo.bar_id = bar.bar_id
but neither work. what am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您可能对
AND
有问题;应该用逗号代替。这样我认为您正在将foo.field1
更新为(bar.field1 AND foo.field2 = bar.field2)
(的非零值之间的逻辑连接) bar.field1
和field2
-s 相等),我很确定这不是您的意思。但没有测试;可能是错的。I think maybe you have problems with
AND
; should be comma instead. This way I think you are updatingfoo.field1
to become(bar.field1 AND foo.field2 = bar.field2)
(logical conjunction between non-zeroness ofbar.field1
and equality offield2
-s), and I'm pretty sure that's not what you meant. Didn't test though; might be wrong.