开发者

MYSQL: update table with values from another related table

开发者 https://www.devze.com 2023-01-18 16:56 出处:网络
I have two MySQL tables, here\'s how they are structured: table foo( foo_id varchar(32), field1 varchar(32),

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?


I think maybe you have problems with AND; should be comma instead. This way I think you are updating foo.field1 to become (bar.field1 AND foo.field2 = bar.field2) (logical conjunction between non-zeroness of bar.field1 and equality of field2-s), and I'm pretty sure that's not what you meant. Didn't test though; might be wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消